zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    [BJOI2019] 光线
    C# 从零开始写 SharpDx 应用 笔刷
    BAT 脚本判断当前系统是 x86 还是 x64 系统
    BAT 脚本判断当前系统是 x86 还是 x64 系统
    win2d 通过 CanvasActiveLayer 画出透明度和裁剪
    win2d 通过 CanvasActiveLayer 画出透明度和裁剪
    PowerShell 拿到显卡信息
    PowerShell 拿到显卡信息
    win10 uwp 如何使用DataTemplate
    win10 uwp 如何使用DataTemplate
  • 原文地址:https://www.cnblogs.com/xinye/p/3296753.html
Copyright © 2011-2022 走看看