Android分享中,如何過濾指定的應用,並且對不同的分享方式發送不同的內容?【轉發】


轉發自德問: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.        
  7.         Intent chooserIntent =Intent.createChooser(intent,"Select app to share");
  8.         if(chooserIntent ==null){
  9.             return;
  10.         }
  11.         try{
  12.             startActivity(chooserIntent);
  13.         }catch(android.content.ActivityNotFoundException ex){
  14.             Toast.makeText(this,"Can't find share component to share",Toast.LENGTH_SHORT).show();
  15.         }

問題:

  1. 一般,通過上面的代碼,提供的分享方式有各種應用:郵件,信息,藍牙,微博,Twitter,二維碼掃描器等。
  2. 但是,第一:我想過濾掉藍牙,
  3. 其次:我想對郵件分享詳細的內容,對信息和微博等分享較簡短的內容,對二維碼掃描器只分享URL。
  4. 請問有什么好的方法達到上述目的?
小飛
小飛
6792
編輯於 2012-03-02
 
 
 
 
評論 (0)       鏈接    2012-02-22 
 
2個答案

 

終於找到解決方法了。

  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.                
  14.                 // judgments : activityInfo.packageName, activityInfo.name, etc.
  15.                 if(activityInfo.packageName.contains("bluetooth")|| activityInfo.name.contains("bluetooth")){
  16.                     continue;
  17.                 }
  18.                 if(activityInfo.packageName.contains("gm")|| activityInfo.name.contains("mail")){
  19.                     targeted.putExtra(Intent.EXTRA_TEXT, contentDetails);
  20.                 }elseif(activityInfo.packageName.contains("zxing")){
  21.                     targeted.putExtra(Intent.EXTRA_TEXT, shareUrl);
  22.                 }else{
  23.                     targeted.putExtra(Intent.EXTRA_TEXT, contentBrief);
  24.                 }
  25.                 targeted.setPackage(activityInfo.packageName);
  26.                 targetedShareIntents.add(targeted);
  27.             }
  28.             Intent chooserIntent =Intent.createChooser(targetedShareIntents.remove(0),"Select app to share");
  29.             if(chooserIntent ==null){
  30.                 return;
  31.             }
  32.             // A Parcelable[] of Intent or LabeledIntent objects as set with
  33.             // putExtra(String, Parcelable[]) of additional activities to place
  34.             // a the front of the list of choices, when shown to the user with a
  35.             // ACTION_CHOOSER.
  36.             chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(newParcelable[]{}));
  37.             try{
  38.                 startActivity(chooserIntent);
  39.             }catch(android.content.ActivityNotFoundException ex){
  40.                 Toast.makeText(this,"Can't find share component to share",Toast.LENGTH_SHORT).show();
  41.             }
  42.         }
小飛
小飛
6792
編輯於   2012-03-02
 
評論 (1)    鏈接  • 2012-02-22
  • 0
    非常好!!!  –  李劍波   2012-02-22
 

過濾掉藍牙的問題,通過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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM