• Android实现自定义分享功能


      效果图如下:

    需求指定应用分享至图片中的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() {
    			@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<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;
    	}



  • 相关阅读:
    WSGI,uwsgi,uWSGI
    彻底关闭win10自动更新
    利用python实现单向链表
    Maven的工程类型有哪些
    Maven仓库是什么
    什么是Maven
    Shiro 的优点
    比较 SpringSecurity 和 Shiro
    shiro有哪些组件
    接口绑定有几种实现方式,分别是怎么实现的
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/2981372.html
Copyright © 2020-2023  润新知