經過intent action指定類型操做(打開文本,網頁,pdf,word,分享...)過濾處理


在android中調用應用打開一個網頁,文本須要經過intent隱士調用打開,可是在調取的應用列表中,又不符合咱們需求的應用或者須要咱們指定部分應用能夠選擇在列表內java

那咱們如何過濾這個列表應用呢?解決的辦法是獲得全部能處理ACTION_的應用程序包名,而後根據名字來過濾或者特殊處理。android

主要用getPackageManager().queryIntentActivities 查詢列表內容,過濾處理。express


下面是我在項目作打開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;
            }

以上是過濾特殊應用,還能夠進行特殊應用特殊處理方式:例如分享,對郵件,短信,微博,微信須要分享不一樣信息,須要分別處理app

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

這個例子只過濾留下facebook,twitter,Gmail分享功能.
wordpress

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);
}
相關文章
相關標籤/搜索