Androidはカスタム共有機能を実現

6878 ワード

効果図は以下の通りです.
需要指定アプリケーションは画像の中の5つのアプリケーションに共有して、携帯電話の上でインストールしていないで灰色に表示して、クリックしてアプリケーション市場を開きます;インストールしたらクリックして開きます.
携帯電話にはウィーチャットしかインストールされていません
   
主なコードは次のとおりです.
	/**
	 *      
	 * 
	 *   5       app,      
	 * 
	 *          
	 * 
	 * @param shareContent
	 */
	public void showShare_5(final String shareContent) {
		dialog = new Dialog(context, R.style.dialog);
		dialog.setContentView(R.layout.dialog_factory_share_5);
		final TextView tv_sina_weibo = (TextView) dialog.findViewById(R.id.tv_sina);
		final TextView tv_renren = (TextView) dialog.findViewById(R.id.tv_renren);
		final TextView tv_tecent_weibo = (TextView) dialog.findViewById(R.id.tv_QQ_weibo);
		final TextView tv_message = (TextView) dialog.findViewById(R.id.tv_message);
		final TextView tv_weixin = (TextView) dialog.findViewById(R.id.tv_weixin);

		final List shareAppInfos = getShareAppList();

		for (int i = 0; i < shareAppInfos.size(); i++) {
			if ("com.sina.weibo".equals(shareAppInfos.get(i).getAppPkgName())) {
				Drawable add = context.getResources().getDrawable(R.drawable.icon_share_sinaweibo);
				add.setBounds(0, 0, add.getMinimumWidth(), add.getMinimumHeight());
				tv_sina_weibo.setCompoundDrawables(add, null, null, null);//      
				tv_sina_weibo.setTag(i);
			}
			if ("com.tencent.WBlog".equals(shareAppInfos.get(i).getAppPkgName())) {
				Drawable add = context.getResources().getDrawable(R.drawable.icon_share_tecent_weibo);
				add.setBounds(0, 0, add.getMinimumWidth(), add.getMinimumHeight());
				tv_tecent_weibo.setCompoundDrawables(add, null, null, null);//      
				tv_tecent_weibo.setTag(i);
			}
			if ("com.android.mms".equals(shareAppInfos.get(i).getAppPkgName())) {
				Drawable add = context.getResources().getDrawable(R.drawable.icon_share_message);
				add.setBounds(0, 0, add.getMinimumWidth(), add.getMinimumHeight());
				tv_message.setCompoundDrawables(add, null, null, null);//      
				tv_message.setTag(i);
			}
			if ("com.renren.mobile.android".equals(shareAppInfos.get(i).getAppPkgName())) {
				Drawable add = context.getResources().getDrawable(R.drawable.icon_share_renren);
				add.setBounds(0, 0, add.getMinimumWidth(), add.getMinimumHeight());
				tv_renren.setCompoundDrawables(add, null, null, null);//      
				tv_renren.setTag(i);
			}
			if ("com.tencent.mm".equals(shareAppInfos.get(i).getAppPkgName())) {
				Drawable add = context.getResources().getDrawable(R.drawable.icon_share_tecent_weixin);
				add.setBounds(0, 0, add.getMinimumWidth(), add.getMinimumHeight());
				tv_weixin.setCompoundDrawables(add, null, null, null);//      
				tv_weixin.setTag(i);
			}
		}
		//     5        
		final AppInfo app_sina = new AppInfo("com.sina.weibo", "com.sina.weibo.EditActivity");
		final AppInfo app_tx_weibo = new AppInfo("com.tencent.WBlog",
				"com.tencent.WBlog.intentproxy.TencentWeiboIntent");
		final AppInfo app_renren = new AppInfo("com.renren.mobile.android",
				"com.renren.mobile.android.publisher.InputPublisherActivity");
		final AppInfo app_message = new AppInfo("com.android.mms", "com.android.mms.ui.ComposeMessageActivity");
		final AppInfo app_weixin = new AppInfo("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");

		final String share = context.getString(R.string.dialog_share1) + shareContent
				+ context.getString(R.string.dialog_share2);

		tv_sina_weibo.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				setOnClick(tv_sina_weibo, app_sina, share);
			}
		});
		tv_renren.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				setOnClick(tv_renren, app_renren, share);
			}
		});
		tv_tecent_weibo.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				setOnClick(tv_tecent_weibo, app_tx_weibo, share);
			}
		});
		tv_message.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				setOnClick(tv_message, app_message, share);
			}
		});
		tv_weixin.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				setOnClick(tv_weixin, app_weixin, share);
			}
		});
		Button btn_close = (Button) dialog.findViewById(R.id.btn_dialog_close);
		btn_close.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		dialog.show();
	}
AppInfo:
public class AppInfo {

		private String		AppPkgName;
		private String		AppLauncherClassName;
		private String		AppName;
		private Drawable	AppIcon;

		public AppInfo() {
			super();
		}

		public AppInfo(String appPkgName, String appLauncherClassName) {
			super();
			AppPkgName = appPkgName;
			AppLauncherClassName = appLauncherClassName;
		}

		public String getAppPkgName() {
			return AppPkgName;
		}

		public void setAppPkgName(String appPkgName) {
			AppPkgName = appPkgName;
		}

		public String getAppLauncherClassName() {
			return AppLauncherClassName;
		}

		public void setAppLauncherClassName(String appLauncherClassName) {
			AppLauncherClassName = appLauncherClassName;
		}

		public String getAppName() {
			return AppName;
		}

		public void setAppName(String appName) {
			AppName = appName;
		}

		public Drawable getAppIcon() {
			return AppIcon;
		}

		public void setAppIcon(Drawable appIcon) {
			AppIcon = appIcon;
		}

	}
	/**
	 *              
	 * 
	 * @param context
	 * @return
	 */
	private List getShareApps(Context context) {
		List mApps = new ArrayList();
		Intent intent = new Intent(Intent.ACTION_SEND, null);
		intent.addCategory(Intent.CATEGORY_DEFAULT);
		intent.setType("text/plain");
		PackageManager pManager = context.getPackageManager();
		mApps = pManager.queryIntentActivities(intent, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
		return mApps;
	}

	/**
	 *       
	 * 
	 * @return
	 */
	private List getShareAppList() {
		List shareAppInfos = new ArrayList();
		PackageManager packageManager = context.getPackageManager();
		List resolveInfos = getShareApps(context);
		if (null == resolveInfos) {
			return null;
		}
		else {
			for (ResolveInfo resolveInfo : resolveInfos) {
				AppInfo appInfo = new AppInfo();
				appInfo.setAppPkgName(resolveInfo.activityInfo.packageName);
				appInfo.setAppLauncherClassName(resolveInfo.activityInfo.name);
				appInfo.setAppName(resolveInfo.loadLabel(packageManager).toString());
				appInfo.setAppIcon(resolveInfo.loadIcon(packageManager));
				shareAppInfos.add(appInfo);
			}
		}
		return shareAppInfos;
	}