Android 分享實現,微信、微博、騰訊 不同分享方式不同內容

來源:互聯網
上載者:User

調用系統的分享方式,沒什麼好說的,只說怎麼針對不同的分享方式分享不同內容,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);

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.