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



    在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);
    }





  • 相关阅读:
    【iBoard电子学堂开发板例程】【12个 stm32 例程发布】
    【iCore双核心组合是开发板例程】【12个 verilog 中级实验例程发布】
    【新产品发布】【iHMI43 智能液晶模块 2013 版】
    【新产品发布】【EVC8001 磁耦隔离式 USB 转 RS-485】
    【iHMI43真彩液晶模块】发布新版 DEMO 软件包,版本号为 0.14
    【新产品发布】【iCore2 ARM / FPGA 双核心板】
    急性淋巴瘤化疗术后高热不退案
    产后便秘案
    药疹治案
    胃癌化疗后急救案
  • 原文地址:https://www.cnblogs.com/happyxiaoyu02/p/6212954.html
Copyright © 2020-2023  润新知