转发自德问:http://www.dewen.org/q/1742
场景: 页面上有一个分享按钮,通过各种分享方式,分享不同的内容。
一般的方式:
- Intent intent =newIntent(Intent.ACTION_SEND);
- intent.setType("text/plain");
- intent.putExtra(Intent.EXTRA_TITLE, title);
- intent.putExtra(Intent.EXTRA_SUBJECT, subject);
- intent.putExtra(Intent.EXTRA_TEXT, content);
- Intent chooserIntent =Intent.createChooser(intent,"Select app to share");
- if(chooserIntent ==null){
- return;
- }
- try{
- startActivity(chooserIntent);
- }catch(android.content.ActivityNotFoundException ex){
- Toast.makeText(this,"Can't find share component to share",Toast.LENGTH_SHORT).show();
- }
问题:
- 一般,通过上面的代码,提供的分享方式有各种应用:邮件,信息,蓝牙,微博,Twitter,二维码扫描器等。
- 但是,第一:我想过滤掉蓝牙,
- 其次:我想对邮件分享详细的内容,对信息和微博等分享较简短的内容,对二维码扫描器只分享URL。
- 请问有什么好的方法达到上述目的?
2个答案
终于找到解决方法了。
- String contentDetails ="";
- String contentBrief ="";
- String shareUrl ="";
- Intent it =newIntent(Intent.ACTION_SEND);
- it.setType("text/plain");
- List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(it,0);
- if(!resInfo.isEmpty()){
- List<Intent> targetedShareIntents =newArrayList<Intent>();
- for(ResolveInfo info : resInfo){
- Intent targeted =newIntent(Intent.ACTION_SEND);
- targeted.setType("text/plain");
- ActivityInfo activityInfo = info.activityInfo;
- // judgments : activityInfo.packageName, activityInfo.name, etc.
- if(activityInfo.packageName.contains("bluetooth")|| activityInfo.name.contains("bluetooth")){
- continue;
- }
- if(activityInfo.packageName.contains("gm")|| activityInfo.name.contains("mail")){
- targeted.putExtra(Intent.EXTRA_TEXT, contentDetails);
- }elseif(activityInfo.packageName.contains("zxing")){
- targeted.putExtra(Intent.EXTRA_TEXT, shareUrl);
- }else{
- targeted.putExtra(Intent.EXTRA_TEXT, contentBrief);
- }
- targeted.setPackage(activityInfo.packageName);
- targetedShareIntents.add(targeted);
- }
- Intent chooserIntent =Intent.createChooser(targetedShareIntents.remove(0),"Select app to share");
- if(chooserIntent ==null){
- return;
- }
- // A Parcelable[] of Intent or LabeledIntent objects as set with
- // putExtra(String, Parcelable[]) of additional activities to place
- // a the front of the list of choices, when shown to the user with a
- // ACTION_CHOOSER.
- chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(newParcelable[]{}));
- try{
- startActivity(chooserIntent);
- }catch(android.content.ActivityNotFoundException ex){
- Toast.makeText(this,"Can't find share component to share",Toast.LENGTH_SHORT).show();
- }
- }
-
0
-
4 票
-
过滤掉蓝牙的问题,通过createchooser方式是没有过滤控制权的。只要设置了ACTION_SEND和text/plain的type,那么系统所有支持这两个元素的应用的会被createchooser收集。因此只能自己过滤,使用packagemanager的queryIntentActivities
- Intent sendIntent =newIntent(Intent.ACTION_SEND);
- sendIntent.setType("text/plain");
- List pkgAppsList = context.getPackageManager().queryIntentActivities( sendIntent,0);
看一个自己使用listview弹出的例子,在此基础上加上过滤的功能
- package com.commonsware.android.launchalot;
- import android.app.ListActivity;
- import android.content.ComponentName;
- import android.content.Intent;
- import android.content.pm.ActivityInfo;
- import android.content.pm.PackageManager;
- import android.content.pm.ResolveInfo;
- import android.os.Bundle;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.ArrayAdapter;
- import android.widget.ImageView;
- import android.widget.ListView;
- import android.widget.TextView;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.List;
- publicclassLaunchalotextendsListActivity{
- AppAdapter adapter=null;
- @Override
- publicvoid onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- PackageManager pm=getPackageManager();
- Intent main=newIntent(Intent.ACTION_MAIN,null);
- main.addCategory(Intent.CATEGORY_LAUNCHER);
- List<ResolveInfo> launchables=pm.queryIntentActivities(main,0);
- Collections.sort(launchables,
- newResolveInfo.DisplayNameComparator(pm));
- adapter=newAppAdapter(pm, launchables);
- setListAdapter(adapter);
- }
- @Override
- protectedvoid onListItemClick(ListView l,View v,
- int position,long id){
- ResolveInfo launchable=adapter.getItem(position);
- ActivityInfo activity=launchable.activityInfo;
- ComponentName name=newComponentName(activity.applicationInfo.packageName,
- activity.name);
- Intent i=newIntent(Intent.ACTION_MAIN);
- i.addCategory(Intent.CATEGORY_LAUNCHER);
- i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
- Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
- i.setComponent(name);
- startActivity(i);
- }
- classAppAdapterextendsArrayAdapter<ResolveInfo>{
- privatePackageManager pm=null;
- AppAdapter(PackageManager pm,List<ResolveInfo> apps){
- super(Launchalot.this, R.layout.row, apps);
- this.pm=pm;
- }
- @Override
- publicView getView(int position,View convertView,
- ViewGroup parent){
- if(convertView==null){
- convertView=newView(parent);
- }
- bindView(position, convertView);
- return(convertView);
- }
- privateView newView(ViewGroup parent){
- return(getLayoutInflater().inflate(R.layout.row, parent,false));
- }
- privatevoid bindView(int position,View row){
- TextView label=(TextView)row.findViewById(R.id.label);
- label.setText(getItem(position).loadLabel(pm));
- ImageView icon=(ImageView)row.findViewById(R.id.icon);
- icon.setImageDrawable(getItem(position).loadIcon(pm));
- }
- }
- }
-
0