Android分享中,如何过滤指定的应用,并且对不同的分享方式发送不同的内容?

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

一般的方式:

Intent intent = new Intent(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();
        }

问题:

  1. 一般,通过上面的代码,提供的分享方式有各种应用:邮件,信息,蓝牙,微博,Twitter,二维码扫描器等。
  2. 但是,第一:我想过滤掉蓝牙,
  3. 其次:我想对邮件分享详细的内容,对信息和微博等分享较简短的内容,对二维码扫描器只分享URL。
  4. 请问有什么好的方法达到上述目的?

终于找到解决方法了。

        String contentDetails = "";
        String contentBrief = "";
        String shareUrl = "";
        Intent it = new Intent(Intent.ACTION_SEND);
        it.setType("text/plain");
        List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(it, 0);
        if (!resInfo.isEmpty()) {
            List<Intent> targetedShareIntents = new ArrayList<Intent>();
            for (ResolveInfo info : resInfo) {
                Intent targeted = new Intent(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);
                } else if (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(new Parcelable[] {}));


            try {
                startActivity(chooserIntent);
            } catch (android.content.ActivityNotFoundException ex) {
                Toast.makeText(this, "Can't find share component to share", Toast.LENGTH_SHORT).show();
            }
        }


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

Intent sendIntent = new Intent(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;


public class Launchalot extends ListActivity {
AppAdapter adapter=null;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


PackageManager pm=getPackageManager();
Intent main=new Intent(Intent.ACTION_MAIN, null);


main.addCategory(Intent.CATEGORY_LAUNCHER);


List<ResolveInfo> launchables=pm.queryIntentActivities(main, 0);


Collections.sort(launchables,
new ResolveInfo.DisplayNameComparator(pm)); 


adapter=new AppAdapter(pm, launchables);
setListAdapter(adapter);
}


@Override
protected void onListItemClick(ListView l, View v,
int position, long id) {
ResolveInfo launchable=adapter.getItem(position);
ActivityInfo activity=launchable.activityInfo;
ComponentName name=new ComponentName(activity.applicationInfo.packageName,
activity.name);
Intent i=new Intent(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);    
}


class AppAdapter extends ArrayAdapter<ResolveInfo> {
private PackageManager pm=null;


AppAdapter(PackageManager pm, List<ResolveInfo> apps) {
super(Launchalot.this, R.layout.row, apps);
this.pm=pm;
}


@Override
public View getView(int position, View convertView,
ViewGroup parent) {
if (convertView==null) {
convertView=newView(parent);
}


bindView(position, convertView);


return(convertView);
}


private View newView(ViewGroup parent) {
return(getLayoutInflater().inflate(R.layout.row, parent, false));
}


private void 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));
}
}
}


原文链接:http://www.dewen.org/q/1742#ans3804

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值
>