Android實現自訂分享功能

來源:互聯網
上載者:User

  如下:

需求指定應用分享至圖片中的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<AppInfo> 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() {@Overridepublic void onClick(View v) {setOnClick(tv_sina_weibo, app_sina, share);}});tv_renren.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {setOnClick(tv_renren, app_renren, share);}});tv_tecent_weibo.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {setOnClick(tv_tecent_weibo, app_tx_weibo, share);}});tv_message.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {setOnClick(tv_message, app_message, share);}});tv_weixin.setOnClickListener(new OnClickListener() {@Overridepublic 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() {@Overridepublic void onClick(View v) {dialog.dismiss();}});dialog.show();}

AppInfo:

 

 

public class AppInfo {private StringAppPkgName;private StringAppLauncherClassName;private StringAppName;private DrawableAppIcon;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<ResolveInfo> getShareApps(Context context) {List<ResolveInfo> mApps = new ArrayList<ResolveInfo>();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<AppInfo> getShareAppList() {List<AppInfo> shareAppInfos = new ArrayList<AppInfo>();PackageManager packageManager = context.getPackageManager();List<ResolveInfo> 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;}

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.