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);  

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值