Android系统分享

本文转自:http://blog.csdn.net/lnb333666/article/details/8824608

[java]  view plain copy print ?
  1. /** 
  2.  * 分享工具类 
  3.  *  
  4.  * @fileName ShareUtil.java 
  5.  * @author naibo-liao 
  6.  * @CreateTime 2013-4-19 上午11:01:04 
  7.  */  
  8. public class ShareUtil {  
  9.     private List<ResolveInfo> resInfo;  
  10.     private PackageManager pm;  
  11.     private Map<String, Boolean> noPicApps;  
  12.     private SettingManager mSettingManager;  
  13.     private static ShareUtil mShareUtil;  
  14.   
  15.     private ShareUtil() {  
  16.         mSettingManager = SettingManager.getInstance(GirlApplication  
  17.                 .getInstance());  
  18.         pm = GirlApplication.getInstance().getPackageManager();  
  19.         getShareNoPicApps();  
  20.     }  
  21.   
  22.     public static ShareUtil getInstance() {  
  23.         if (mShareUtil == null) {  
  24.             mShareUtil = new ShareUtil();  
  25.         }  
  26.         return mShareUtil;  
  27.     }  
  28.     /** 
  29.      * 获取分享目标 
  30.      *  
  31.      * @return 
  32.      */  
  33.     public List<Intent> getTargetedShareIntents(String picUrl) {  
  34.         try {  
  35.   
  36.             String path = FileUtils.copyFile(picUrl);  
  37.   
  38.             File file = new File(path);  
  39.             Intent intent = new Intent(Intent.ACTION_SEND);  
  40.             intent.setType("text/plain");  
  41.             if (null == resInfo) {  
  42.                 resInfo = pm.queryIntentActivities(intent, 0);  
  43.             }  
  44.             if (!resInfo.isEmpty()) {  
  45.                 List<Intent> targetedShareIntents = new ArrayList<Intent>();  
  46.                 for (ResolveInfo info : resInfo) {  
  47.                     Intent targeted = new Intent(Intent.ACTION_SEND);  
  48.   
  49.                     /** 
  50.                      * 以下的代码大致可以理解为 获取本地有分享动作的应用,如微信、微博等, 
  51.                      * 根据包名或应用名来分配不同的内容。 
  52.                      */  
  53.                     ActivityInfo activityInfo = info.activityInfo;  
  54.                     String packageName = activityInfo.packageName;  
  55.                     PackageInfo pinfo = pm.getPackageInfo(packageName, 0);  
  56.                     String appName = pinfo.applicationInfo.loadLabel(pm)  
  57.                             .toString();  
  58.   
  59.                     if (null != noPicApps && noPicApps.size() > 0) {  
  60.                         //需要特别处理分享内容的应用,由服务器派发  
  61.                         if (noPicApps.containsKey(appName.toUpperCase())) {  
  62.                             targeted.setType("text/plain");  
  63.                         } else {  
  64.                             targeted.setType("image/*");  
  65.                             targeted.putExtra(Intent.EXTRA_STREAM,  
  66.                                     Uri.fromFile(file));  
  67.                         }  
  68.                     } else {  
  69.                         //需要特别处理分享内容的应用,本地自行判断,代码有点多余了,哈哈  
  70.                         if (appName.contains("微信") || appName.contains("UC")) {  
  71.                             targeted.setType("text/plain");  
  72.                         } else {  
  73.                             targeted.setType("image/*");  
  74.                             targeted.putExtra(Intent.EXTRA_STREAM,  
  75.                                     Uri.fromFile(file));  
  76.                         }  
  77.                     }  
  78.   
  79.                     //分享的文本信息  
  80.                     targeted.putExtra(Intent.EXTRA_TEXT,  
  81.                             reconfigurationShareMessage(appName));  
  82.                     // targeted.setClassName(getApplication(), className);  
  83.                     targeted.setPackage(packageName);  
  84.                     targetedShareIntents.add(targeted);  
  85.                 }  
  86.                 return targetedShareIntents;  
  87.             }  
  88.         } catch (Exception e) {  
  89.         }  
  90.         return null;  
  91.     }  
  92.   
  93.     /**  
  94.      * 获取不需要分享图片的应用  
  95.      */  
  96.     private void getShareNoPicApps() {  
  97.         if (noPicApps == null) {  
  98.             noPicApps = new HashMap<String, Boolean>();  
  99.             if (!TextUtils.isEmpty(mSettingManager.getShareNoPicApps())) {  
  100.                 String[] strNoPicApps = mSettingManager.getShareNoPicApps()  
  101.                         .split(",");  
  102.                 for (int i = 0; i < strNoPicApps.length; i++) {  
  103.                     noPicApps.put(strNoPicApps[i].toUpperCase(), true);  
  104.                 }  
  105.             }  
  106.         }  
  107.     }  
  108.   
  109.     /** 
  110.      * 构建分享内容 
  111.      *  
  112.      * @param appName 
  113.      * @return 
  114.      */  
  115.     private String reconfigurationShareMessage(String appName) {  
  116.         String message = mSettingManager.getShareMessage();  
  117.         String shareUrl = mSettingManager.getShareUrl();  
  118.         String encode = "frapp="  
  119.                 + URLEncoder.encode(appName)  
  120.                 + "&frid="  
  121.                 + EncryptUtils.encryptToMD5(NetParameters.IMEI)  
  122.                         .substring(824);  
  123.         if (!TextUtils.isEmpty(shareUrl)) {  
  124.             if (shareUrl.contains("?")) {  
  125.                 shareUrl = shareUrl + "&" + encode;  
  126.             } else {  
  127.                 shareUrl = shareUrl + "?" + encode;  
  128.             }  
  129.         }  
  130.         if (message.contains("{#shareurl#}") && !TextUtils.isEmpty(shareUrl)) {  
  131.             return message.replace("{#shareurl#}", shareUrl);  
  132.         }  
  133.         return message;  
  134.     }  
  135. }  
[java]  view plain copy print ?
  1. List<Intent> targetedShareIntents = ShareUtil.getInstance()  
  2.                     .getTargetedShareIntents(picdetail.getPictureurl());  
  3.             // 分享框标题  
  4.             Intent chooserIntent = Intent.createChooser(  
  5.                     targetedShareIntents.remove(0), "分享给好友");  
  6.             if (chooserIntent == null) {  
  7.                 return;  
  8.             }  
  9.   
  10.             chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,  
  11.                     targetedShareIntents.toArray(new Parcelable[]{}));  
  12.             chooserIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  13.             startActivity(chooserIntent);  

你可能感兴趣的:(Android系统分享)