Android(安卓)分享实现,微信、微博、腾讯 不同分享方式不同内容

调用系统的分享方式,没什么好说的,只说怎么针对不同的分享方式分享不同内容,demo代码贴上,有更好方式的哥们可以留言一下。


/** * 分享工具类 *  * @fileName ShareUtil.java * @author naibo-liao * @CreateTime 2013-4-19 上午11:01:04 */public class ShareUtil {private List<ResolveInfo> resInfo;private PackageManager pm;private Map<String, Boolean> noPicApps;private SettingManager mSettingManager;private static ShareUtil mShareUtil;private ShareUtil() {mSettingManager = SettingManager.getInstance(GirlApplication.getInstance());pm = GirlApplication.getInstance().getPackageManager();getShareNoPicApps();}public static ShareUtil getInstance() {if (mShareUtil == null) {mShareUtil = new ShareUtil();}return mShareUtil;}/** * 获取分享目标 *  * @return */public List<Intent> getTargetedShareIntents(String picUrl) {try {String path = FileUtils.copyFile(picUrl);File file = new File(path);Intent intent = new Intent(Intent.ACTION_SEND);intent.setType("text/plain");if (null == resInfo) {resInfo = pm.queryIntentActivities(intent, 0);}if (!resInfo.isEmpty()) {List<Intent> targetedShareIntents = new ArrayList<Intent>();for (ResolveInfo info : resInfo) {Intent targeted = new Intent(Intent.ACTION_SEND);/** * 以下的代码大致可以理解为 获取本地有分享动作的应用,如微信、微博等, * 根据包名或应用名来分配不同的内容。 */ActivityInfo activityInfo = info.activityInfo;String packageName = activityInfo.packageName;PackageInfo pinfo = pm.getPackageInfo(packageName, 0);String appName = pinfo.applicationInfo.loadLabel(pm).toString();if (null != noPicApps && noPicApps.size() > 0) {//需要特别处理分享内容的应用,由服务器派发if (noPicApps.containsKey(appName.toUpperCase())) {targeted.setType("text/plain");} else {targeted.setType("image/*");targeted.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(file));}} else {//需要特别处理分享内容的应用,本地自行判断,代码有点多余了,哈哈if (appName.contains("微信") || appName.contains("UC")) {targeted.setType("text/plain");} else {targeted.setType("image/*");targeted.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(file));}}//分享的文本信息targeted.putExtra(Intent.EXTRA_TEXT,reconfigurationShareMessage(appName));// targeted.setClassName(getApplication(), className);targeted.setPackage(packageName);targetedShareIntents.add(targeted);}return targetedShareIntents;}} catch (Exception e) {}return null;}/** * 获取不需要分享图片的应用 */private void getShareNoPicApps() {if (noPicApps == null) {noPicApps = new HashMap<String, Boolean>();if (!TextUtils.isEmpty(mSettingManager.getShareNoPicApps())) {String[] strNoPicApps = mSettingManager.getShareNoPicApps().split(",");for (int i = 0; i < strNoPicApps.length; i++) {noPicApps.put(strNoPicApps[i].toUpperCase(), true);}}}}/** * 构建分享内容 *  * @param appName * @return */private String reconfigurationShareMessage(String appName) {String message = mSettingManager.getShareMessage();String shareUrl = mSettingManager.getShareUrl();String encode = "frapp="+ URLEncoder.encode(appName)+ "&frid="+ EncryptUtils.encryptToMD5(NetParameters.IMEI).substring(8, 24);if (!TextUtils.isEmpty(shareUrl)) {if (shareUrl.contains("?")) {shareUrl = shareUrl + "&" + encode;} else {shareUrl = shareUrl + "?" + encode;}}if (message.contains("{#shareurl#}") && !TextUtils.isEmpty(shareUrl)) {return message.replace("{#shareurl#}", shareUrl);}return message;}}


List<Intent> targetedShareIntents = ShareUtil.getInstance().getTargetedShareIntents(picdetail.getPictureurl());// 分享框标题Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "分享给好友");if (chooserIntent == null) {return;}chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,targetedShareIntents.toArray(new Parcelable[]{}));chooserIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);startActivity(chooserIntent);