微信微博分享功能

默认分享(微信、新浪微博、腾讯微博、网易微博、人人网)

微信微博分享功能_第1张图片

import java.util.ArrayList;
import java.util.List;

import android.R;
import android.app.Activity;
import android.app.Dialog;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ShareActivity extends Activity {

	private Dialog mDialog;
	private ListView mList;
	private ShareAdapter mAdapter = new ShareAdapter();
	private String[] packages = { "com.tencent.mm", "com.sina.weibo",
			"com.tencent.WBlog", "com.netease.wb", "com.renren.mobile.android" };
	private String[] actions = { "com.tencent.mm.ui.tools.ShareImgUI",
			"com.sina.weibo.EditActivity",
			"com.tencent.WBlog.intentproxy.TencentWeiboIntent",
			"com.netease.wb.activity.SendBlogActivity",
			"com.renren.mobile.android.publisher.InputPublisherActivity" };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getShareApp();
	}

	public void onRightFunO(View view) {
		if (mAdapter.getCount() > 1) {
			showDialog();
		} else {
			share(null);
		}
	}

	private void share(AppInfo app) {
		try {
			Intent intent = new Intent();
			if (app != null && app.pkg != null) {
				ComponentName comp = new ComponentName(app.pkg, app.action);
				intent.setComponent(comp);
			}
			intent.setAction(Intent.ACTION_SEND);
			intent.setType("text/plain");
			intent.putExtra(Intent.EXTRA_SUBJECT, "分享");
			intent.putExtra(Intent.EXTRA_TEXT, "快讯详情");
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			startActivity(intent);
		} catch (Exception e) {
			Toast.makeText(this, "没有可执行的应用", Toast.LENGTH_LONG).show();
		}
	}

	private void getShareApp() {
		Intent intent = new Intent(Intent.ACTION_SEND, null);
		intent.addCategory(Intent.CATEGORY_DEFAULT);
		intent.setType("text/plain");

		PackageManager packageManager = this.getPackageManager();
		List<ResolveInfo> mApps = packageManager.queryIntentActivities(intent,
				PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
		int count = packages.length;
		List<AppInfo> data = new ArrayList<ShareActivity.AppInfo>();
		for (ResolveInfo resolveInfo : mApps) {
			for (int i = 0; i < count; i++) {
				if (resolveInfo.activityInfo.packageName
						.equalsIgnoreCase(packages[i])) {
					AppInfo app = new AppInfo();
					app.label = resolveInfo.loadLabel(packageManager)
							.toString();
					app.pkg = packages[i];
					app.action = actions[i];
					app.icon = resolveInfo.loadIcon(packageManager);
					data.add(app);
				}
			}
		}
		AppInfo app = new AppInfo();
		app.label = "更多";
		app.icon = getResources().getDrawable(R.drawable.ic_more);
		data.add(app);
		mAdapter.setData(data);
	}

	private void showDialog() {
		if (mDialog == null) {
			mDialog = new Dialog(this, R.style.dialog);
			View contentView = inflater.inflate(R.layout.filter_dropdown, null);

			mList = (ListView) contentView.findViewById(R.id.filter_list);
			mList.setItemsCanFocus(false);
			mList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
			TextView mTitleBarLabel = (TextView) contentView
					.findViewById(R.id.title_bar_label);
			mTitleBarLabel.setText("分享");
			mDialog.setContentView(contentView);
			mList.setAdapter(mAdapter);
		}

		mList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				mDialog.dismiss();
				share((AppInfo) parent.getAdapter().getItem(position));
			}
		});
		mDialog.show();
	}

	private class AppInfo {
		public String label;
		private String pkg;
		public String action;
		public Drawable icon;
	}

	private class ShareAdapter extends BaseAdapter {

		private List<AppInfo> apps = new ArrayList<ShareActivity.AppInfo>();

		public void setData(final List<ShareActivity.AppInfo> app) {
			this.apps = app;
			notifyDataSetChanged();
		}

		@Override
		public int getCount() {
			return apps.size();
		}

		@Override
		public Object getItem(int position) {
			return apps.get(position);
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.menu_item, null);
			}
			AppInfo app = apps.get(position);
			ImageView img = (ImageView) convertView.findViewById(R.id.image1);
			img.setImageDrawable(app.icon);
			TextView text1 = (TextView) convertView.findViewById(R.id.text1);
			text1.setText(app.label);

			return convertView;
		}
	}
}


你可能感兴趣的:(微信微博分享功能)