关闭

通过intent action指定类型操作(打开文本,网页,pdf,word,分享...)过滤处理

标签: android
2751人阅读 评论(1) 收藏 举报
分类:


在android中调用应用打开一个网页,文本需要通过intent隐士调用打开,但是在调取的应用列表中,又不符合我们需求的应用或者需要我们指定部分应用可以选择在列表内

那我们如何过滤这个列表应用呢?解决的办法是得到所有能处理ACTION_的应用程序包名,然后根据名字来过滤或者特殊处理。

主要用getPackageManager().queryIntentActivities 查询列表内容,过滤处理。


下面是我在项目做打开pdf文件时过滤掉QQ app在列表选项部分代码:

 Intent it = new Intent();
            it.setAction(android.content.Intent.ACTION_VIEW);
            it.setDataAndType(Uri.fromFile(electronInvoiceFile), "application/pdf");
            it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(it, 0);
         //   int removeAppIndex=-1;
            if (!resInfo.isEmpty()) {
                List<Intent> targetedIntents = new ArrayList<Intent>();
                for (int n=0;n<resInfo.size();n++) {
                    ResolveInfo info =resInfo.get(n);
                    String pkg=info.activityInfo.packageName.toLowerCase();
                    //特殊处理,过滤qq等应用不可用选项,com.tencent.mm /mobileqq
                    if (!pkg.contains("com.tencent")) {
                        Intent chit = new Intent();
                        chit.setPackage(pkg);
                        chit.setAction(android.content.Intent.ACTION_VIEW);
                        chit.setDataAndType(Uri.fromFile(electronInvoiceFile), "application/pdf");
                        targetedIntents.add(chit);
                    }
                }

                /*if(removeAppIndex!=-1) {
                    resInfo.remove(removeAppIndex);
                }*/
                Intent chooserIntent = Intent.createChooser(targetedIntents.remove(0), "选择浏览方式");
                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, targetedIntents.toArray(new Parcelable[]{}));
                chooserIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                try {
                    getApplicationContext().startActivity(chooserIntent);
                }catch (ActivityNotFoundException e){
                    e.printStackTrace();
                    BDebug.e(getClass().getSimpleName(), "open pdf choose not found");
                    CommonUtility.showToast(getApplicationContext(),"打开pdf失败,请安装pdf查看器后在进行查看!");
                }

            }else{
                CommonUtility.showToast(getApplicationContext(),"打开pdf失败,请安装pdf查看器后在进行查看!");
                return;
            }

以上是过滤特殊应用,还可以进行特殊应用特殊处理方式:例如分享,对邮件,短信,微博,微信需要分享不同信息,需要分别处理

来源:https://trivedihardik.wordpress.com/2013/10/29/app-chooser/

这个例子只过滤留下facebook,twitter,Gmail分享功能.

private void onShareFiltered() {
	Intent emailIntent = new Intent();
	emailIntent.setAction(Intent.ACTION_SEND);
	// Native email client doesn't currently support HTML, but it doesn't
	// hurt to try in case they fix it
	emailIntent.putExtra(Intent.EXTRA_TEXT, shareText.getText().toString());
	emailIntent.putExtra(Intent.EXTRA_SUBJECT,
			getString(R.string.intent_subject));
	emailIntent.setType("message/rfc822");
		PackageManager pm = getPackageManager();
	Intent sendIntent = new Intent(Intent.ACTION_SEND);
	sendIntent.setType("text/plain");
	Intent openInChooser = Intent.createChooser(emailIntent,
			getString(R.string.app_name)
			+ getString(R.string.share_message));
	List resInfo = pm.queryIntentActivities(sendIntent, 0);
	List intentList = new ArrayList();
	for (int i = 0; i < resInfo.size(); i++) {
		// Extract the label, append it, and repackage it in a LabeledIntent
		ResolveInfo ri = resInfo.get(i);
		String packageName = ri.activityInfo.packageName;
		if (packageName.contains("android.email")) {
			emailIntent.setPackage(packageName);
		} else if (packageName.contains("twitter")
			|| packageName.contains("facebook")) {
			Intent intent = new Intent();
			intent.setComponent(new ComponentName(packageName,
					ri.activityInfo.name));
			intent.setAction(Intent.ACTION_SEND);
			intent.setType("text/plain");
			if (packageName.contains("twitter")) {
				intent.putExtra(Intent.EXTRA_TEXT, shareText.getText()
					.toString());
			} else if (packageName.contains("facebook")) {
				// Warning: Facebook IGNORES our text. They say
				// "These fields are intended for users to express themselves. 
                                //Pre-filling these fields erodes the authenticity of the user voice."
				// One workaround is to use the Facebook SDK to post, but
				// that doesn't allow the user to choose how they want to
				// share. We can also make a custom landing page, and the
				// link
				// will show the  text from that page
				// with our link in Facebook.
				intent.putExtra(Intent.EXTRA_TEXT, shareText.getText()
						.toString());
			} else if (packageName.contains("android.gm")) {
				intent.putExtra(Intent.EXTRA_TEXT, shareText.getText()
					.toString());
				intent.putExtra(Intent.EXTRA_SUBJECT,
					getString(R.string.intent_subject));
				intent.setType("message/rfc822");
			}
			intentList.add(new LabeledIntent(intent, packageName, ri
				.loadLabel(pm), ri.icon));
			}
		}

	// convert intentList to array
	LabeledIntent[] extraIntents = intentList
		.toArray(new LabeledIntent[intentList.size()]);
	openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
	startActivity(openInChooser);
}





0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:642506次
    • 积分:4247
    • 等级:
    • 排名:第7126名
    • 原创:38篇
    • 转载:1篇
    • 译文:0篇
    • 评论:143条
    博客专栏
    最新评论