转发自德问:http://www.dewen.org/q/1742

场景: 页面上有一个分享按钮,通过各种分享方式,分享不同的内容。

一般的方式:

  1. Intent intent =newIntent(Intent.ACTION_SEND);
  2. intent.setType("text/plain");
  3. intent.putExtra(Intent.EXTRA_TITLE, title);
  4. intent.putExtra(Intent.EXTRA_SUBJECT, subject);
  5. intent.putExtra(Intent.EXTRA_TEXT, content);
  6. Intent chooserIntent =Intent.createChooser(intent,"Select app to share");
  7. if(chooserIntent ==null){
  8. return;
  9. }
  10. try{
  11. startActivity(chooserIntent);
  12. }catch(android.content.ActivityNotFoundException ex){
  13. Toast.makeText(this,"Can't find share component to share",Toast.LENGTH_SHORT).show();
  14. }

问题:

  1. 一般,通过上面的代码,提供的分享方式有各种应用:邮件,信息,蓝牙,微博,Twitter,二维码扫描器等。
  2. 但是,第一:我想过滤掉蓝牙,
  3. 其次:我想对邮件分享详细的内容,对信息和微博等分享较简短的内容,对二维码扫描器只分享URL。
  4. 请问有什么好的方法达到上述目的?
小飞
6792
编辑于 2012-03-02 评论 (0) 分享 链接 2012-02-22  2个答案 票 数

  • JK

    5 票

  • 400
  • 最佳答案

终于找到解决方法了。

  1. String contentDetails ="";
  2. String contentBrief ="";
  3. String shareUrl ="";
  4. Intent it =newIntent(Intent.ACTION_SEND);
  5. it.setType("text/plain");
  6. List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(it,0);
  7. if(!resInfo.isEmpty()){
  8. List<Intent> targetedShareIntents =newArrayList<Intent>();
  9. for(ResolveInfo info : resInfo){
  10. Intent targeted =newIntent(Intent.ACTION_SEND);
  11. targeted.setType("text/plain");
  12. ActivityInfo activityInfo = info.activityInfo;
  13. // judgments : activityInfo.packageName, activityInfo.name, etc.
  14. if(activityInfo.packageName.contains("bluetooth")|| activityInfo.name.contains("bluetooth")){
  15. continue;
  16. }
  17. if(activityInfo.packageName.contains("gm")|| activityInfo.name.contains("mail")){
  18. targeted.putExtra(Intent.EXTRA_TEXT, contentDetails);
  19. }elseif(activityInfo.packageName.contains("zxing")){
  20. targeted.putExtra(Intent.EXTRA_TEXT, shareUrl);
  21. }else{
  22. targeted.putExtra(Intent.EXTRA_TEXT, contentBrief);
  23. }
  24. targeted.setPackage(activityInfo.packageName);
  25. targetedShareIntents.add(targeted);
  26. }
  27. Intent chooserIntent =Intent.createChooser(targetedShareIntents.remove(0),"Select app to share");
  28. if(chooserIntent ==null){
  29. return;
  30. }
  31. // A Parcelable[] of Intent or LabeledIntent objects as set with
  32. // putExtra(String, Parcelable[]) of additional activities to place
  33. // a the front of the list of choices, when shown to the user with a
  34. // ACTION_CHOOSER.
  35. chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(newParcelable[]{}));
  36. try{
  37. startActivity(chooserIntent);
  38. }catch(android.content.ActivityNotFoundException ex){
  39. Toast.makeText(this,"Can't find share component to share",Toast.LENGTH_SHORT).show();
  40. }
  41. }
小飞
6792
编辑于 2012-03-02 评论 (1) 链接 • 2012-02-22
  • 0 非常好!!! – 李剑波 2012-02-22
  • 李剑波

    4 票

  • 4768

过滤掉蓝牙的问题,通过createchooser方式是没有过滤控制权的。只要设置了ACTION_SEND和text/plain的type,那么系统所有支持这两个元素的应用的会被createchooser收集。因此只能自己过滤,使用packagemanager的queryIntentActivities

  1. Intent sendIntent =newIntent(Intent.ACTION_SEND);
  2. sendIntent.setType("text/plain");
  3. List pkgAppsList = context.getPackageManager().queryIntentActivities( sendIntent,0);

看一个自己使用listview弹出的例子,在此基础上加上过滤的功能

  1. package com.commonsware.android.launchalot;
  2. import android.app.ListActivity;
  3. import android.content.ComponentName;
  4. import android.content.Intent;
  5. import android.content.pm.ActivityInfo;
  6. import android.content.pm.PackageManager;
  7. import android.content.pm.ResolveInfo;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import android.widget.ArrayAdapter;
  12. import android.widget.ImageView;
  13. import android.widget.ListView;
  14. import android.widget.TextView;
  15. import java.util.Collections;
  16. import java.util.Comparator;
  17. import java.util.List;
  18. publicclassLaunchalotextendsListActivity{
  19. AppAdapter adapter=null;
  20. @Override
  21. publicvoid onCreate(Bundle savedInstanceState){
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.main);
  24. PackageManager pm=getPackageManager();
  25. Intent main=newIntent(Intent.ACTION_MAIN,null);
  26. main.addCategory(Intent.CATEGORY_LAUNCHER);
  27. List<ResolveInfo> launchables=pm.queryIntentActivities(main,0);
  28. Collections.sort(launchables,
  29. newResolveInfo.DisplayNameComparator(pm));
  30. adapter=newAppAdapter(pm, launchables);
  31. setListAdapter(adapter);
  32. }
  33. @Override
  34. protectedvoid onListItemClick(ListView l,View v,
  35. int position,long id){
  36. ResolveInfo launchable=adapter.getItem(position);
  37. ActivityInfo activity=launchable.activityInfo;
  38. ComponentName name=newComponentName(activity.applicationInfo.packageName,
  39. activity.name);
  40. Intent i=newIntent(Intent.ACTION_MAIN);
  41. i.addCategory(Intent.CATEGORY_LAUNCHER);
  42. i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
  43. Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
  44. i.setComponent(name);
  45. startActivity(i);
  46. }
  47. classAppAdapterextendsArrayAdapter<ResolveInfo>{
  48. privatePackageManager pm=null;
  49. AppAdapter(PackageManager pm,List<ResolveInfo> apps){
  50. super(Launchalot.this, R.layout.row, apps);
  51. this.pm=pm;
  52. }
  53. @Override
  54. publicView getView(int position,View convertView,
  55. ViewGroup parent){
  56. if(convertView==null){
  57. convertView=newView(parent);
  58. }
  59. bindView(position, convertView);
  60. return(convertView);
  61. }
  62. privateView newView(ViewGroup parent){
  63. return(getLayoutInflater().inflate(R.layout.row, parent,false));
  64. }
  65. privatevoid bindView(int position,View row){
  66. TextView label=(TextView)row.findViewById(R.id.label);
  67. label.setText(getItem(position).loadLabel(pm));
  68. ImageView icon=(ImageView)row.findViewById(R.id.icon);
  69. icon.setImageDrawable(getItem(position).loadIcon(pm));
  70. }
  71. }
  72. }
评论 (1) 链接 • 2012-02-22
  • 0 见过一些应用,好像不需要自定义ListView,直接使用API的。我再找找方法。。。 – JK 2012-02-22

更多相关文章

  1. 【Android】监听蓝牙状态变化
  2. 【Android开发 蓝牙连接状态】Android实时检测蓝牙连接状态
  3. Android—使用ShareSDK实现新浪微博分享
  4. Android(安卓)Intent设置类型setType();
  5. 我的第一个Android程序,BMI计算器,分享一下源码
  6. Android(安卓)bluetooth 开发
  7. Android蓝牙的开启-搜索-关闭演示
  8. android启动蓝牙的过程 (高通方案)
  9. 分享:Android(安卓)-- Properties使用

随机推荐

  1. Android(安卓)是否前台运行
  2. android之显示Log
  3. Android(安卓)Retrofit 2.0框架上传图片,
  4. android发送post请求出现问题
  5. android 连接USB按power键锁屏2声锁屏音
  6. Android(安卓)dialog 去除虚拟按键
  7. Android中文API(142) —— Gravity
  8. 总结android音频视频操作
  9. android intent.setType("type");的含义
  10. linux 配置安装android sdk自动下载缺少