空意图选择器(没有应用程序可以执行此操作)意图、应用程序、操作、选择器

2023-09-06 16:22:19 作者:撩妹一把手

我intentchooser是白名单(只有一个选择applictions将在意向选择器显示出来)的基础上。在code是另一个code至极的基础上,请问oposite;列入黑名单的应用程序。我从这里和this是相关的讨论涉及它

语境下如何选择器被创建:

 的String []白名单=新的String [] {org.schiphol,nl.negentwee,org.schipholsecurity,org.chineseschiphol,nl.ns ,com.tomtom};

    意向意图=新的意图(Intent.ACTION_MAIN);
    //intent.addCategory(Intent.CATEGORY_LAUNCHER);

    startActivity(generateCustomChooserIntent(意向,白名单));
 

我用它来创建白名单选择器的方法;

  //方法:
私人意图generateCustomChooserIntent(意图原型的String []白名单){
    名单<意图> targetedShareIntents =新的ArrayList<意图>();
    名单< HashMap的<字符串,字符串>> intentMetaInfo =新的ArrayList< HashMap的<字符串,字符串>>();
    意图chooserIntent;

    意图哑=新的意图(prototype.getAction());
    dummy.setType(prototype.getType());
    名单< ResolveInfo> 。resInfo = getPackageManager()queryIntentActivities(假,0);
    MyLog.i(LOG_TAG,安装在设备的应用程序:+ resInfo.size());

    如果(!resInfo.isEmpty()){
        对于(ResolveInfo resolveInfo:resInfo){
            // MyLog.i(LOG_TAG,看:+ resolveInfo.activityInfo.packageName);

            如果(resolveInfo.activityInfo == NULL){
                MyLog.e(LOG_TAG,解决应用程序有没有活动的信息,所以无法使用。);
                继续;
            }

            如果(Arrays.asList(白名单)。载(resolveInfo.activityInfo.packageName)){
                // MyLog.i(LOG_TAG,=============================>接受了);

                HashMap的<字符串,字符串>信息=新的HashMap<字符串,字符串>();
                info.put(软件包名,resolveInfo.activityInfo.packageName);
                info.put(类名,resolveInfo.activityInfo.name);
                info.put(simpleName,将String.valueOf(resolveInfo.activityInfo.loadLabel(getPackageManager())));
                intentMetaInfo.add(信息);
            } 其他 {
                // MyLog.i(LOG_TAG,拒绝);
            }
        }

        如果(!intentMetaInfo.isEmpty()){
            MyLog.i(LOG_TAG---完成编制清单---);

            // TODO再次启用排序
            //排序的不错的可读性
            // Collections.sort(intentMetaInfo,新的比较< HashMap的<字符串,字符串>>(){
            // @覆盖
            //公众诠释比较(HashMap的<字符串,字符串>地图,HashMap的<字符串,字符串> MAP2){
            //返回map.get(simpleName)的compareTo(map2.get(simpleName))。
            //}
            //});

            MyLog.i(LOG_TAG ---创建自定义的意图列表---);

            //创建自定义的意图列表
            对(HashMap的<字符串,字符串>元信息:intentMetaInfo){
                MyLog.i(LOG_TAG,并称+ metaInfo.get(软件包名)+的意向清单);

                意向targetedShareIntent =(意向)prototype.clone();
                targetedShareIntent.setPackage(metaInfo.get(软件包名));
                targetedShareIntent.setClassName(metaInfo.get(软件包名),metaInfo.get(类名));
                targetedShareIntents.add(targetedShareIntent);
            }

            MyLog.i(LOG_TAG,---做编译意图列表---);
            MyLog.i(LOG_TAG,总数targetedShareIntents:+ targetedShareIntents.size());

            chooserIntent = Intent.createChooser(targetedShareIntents.remove(targetedShareIntents.size() -  1),Selecteer雷斯应用程序(1));
            MyLog.i(LOG_TAG ---创建选择器---);
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,targetedShareIntents.toArray(新Parcelable [] {}));

            MyLog.e(LOG_TAG回乡填补(自定义)选择器);
            返回chooserIntent;
        }
    }

    MyLog.e(LOG_TAG,返回默认选择器(空));
    返回Intent.createChooser(原型,Selecteer REIS应用程序);
}
 

现在什么happends是,结果选择器显示没有应用程序可以执行此操作虽然logcat中显示有选定5的应用程序。

LogCat中已记录的结果:

  06-28 13:04:48.679:I / NavigationTypeActivity(9400):安装在设备上的应用程序:356
06-28 13:04:48.687:I / NavigationTypeActivity(9400):---完成编译列表---
06-28 13:04:48.687:I / NavigationTypeActivity(9400):---创建自定义的意图列表---
06-28 13:04:48.687:I / NavigationTypeActivity(9400):加入org.chineseschiphol的意图列表
06-28 13:04:48.687:I / NavigationTypeActivity(9400):加入org.schiphol的意图列表
06-28 13:04:48.687:I / NavigationTypeActivity(9400):加入org.schipholsecurity的意图列表
06-28 13:04:48.687:I / NavigationTypeActivity(9400):加入nl.negentwee的意图列表
06-28 13:04:48.687:I / NavigationTypeActivity(9400):---完成编译意图列表---
06-28 13:04:48.687:I / NavigationTypeActivity(9400):总数targetedShareIntents:4
06-28 13:04:48.687:I / NavigationTypeActivity(9400):---选择器创建---
06-28 13:04:48.687:E / NavigationTypeActivity(9400):返回填充(自定义)选择器
 
没有应用程序可执行此操作 是什么意思

解决方案

我从一个Android大师的一些反馈,告诉我应该工作的意图来定义的选择器,例如playstore或Gmail什么的。从理论上讲,你可以提供自己的应用程序的起始意图(你确定你自己的应用程序安装)。

 意图chooserIntent = Intent.createChooser(新意图(Intent.ACTION_VIEW,Uri.parse(市场://细节ID = Gmail的)),榜样);
 

后来,这就是为什么名单从列表中删除单个项目(你可能不希望在选配自己的应用程序)。

My intentchooser is on the basis of a whitelist (only a selection of applictions will show up in the intent chooser). The code is on the basis of another code wich does the oposite; blacklisting applications. I got that code from here and this is the related discussion related to it.

Context of how the chooser gets created:

        String[] whitelist = new String[] { "org.schiphol", "nl.negentwee", "org.schipholsecurity", "org.chineseschiphol", "nl.ns", "com.tomtom" };

    Intent intent = new Intent(Intent.ACTION_MAIN);
    //intent.addCategory(Intent.CATEGORY_LAUNCHER);

    startActivity(generateCustomChooserIntent(intent, whitelist));

The method I use to create the whitelisted chooser;

// Method:
private Intent generateCustomChooserIntent(Intent prototype, String[] whiteList) {
    List<Intent> targetedShareIntents = new ArrayList<Intent>();
    List<HashMap<String, String>> intentMetaInfo = new ArrayList<HashMap<String, String>>();
    Intent chooserIntent;

    Intent dummy = new Intent(prototype.getAction());
    dummy.setType(prototype.getType());
    List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(dummy, 0);
    MyLog.i(LOG_TAG, "Apps installed on device:" + resInfo.size());

    if (!resInfo.isEmpty()) {
        for (ResolveInfo resolveInfo : resInfo) {
            // MyLog.i(LOG_TAG, "Looking at:" + resolveInfo.activityInfo.packageName);

            if (resolveInfo.activityInfo == null) {
                MyLog.e(LOG_TAG, "resolved application has no activity info, so it is not usable.");
                continue;
            }

            if (Arrays.asList(whiteList).contains(resolveInfo.activityInfo.packageName)) {
                // MyLog.i(LOG_TAG, "=============================> accepted");

                HashMap<String, String> info = new HashMap<String, String>();
                info.put("packageName", resolveInfo.activityInfo.packageName);
                info.put("className", resolveInfo.activityInfo.name);
                info.put("simpleName", String.valueOf(resolveInfo.activityInfo.loadLabel(getPackageManager())));
                intentMetaInfo.add(info);
            } else {
                // MyLog.i(LOG_TAG, "rejected");
            }
        }

        if (!intentMetaInfo.isEmpty()) {
            MyLog.i(LOG_TAG, "--- done compiling list ---");

            // TODO enable sorting again
            // sorting for nice readability
            // Collections.sort(intentMetaInfo, new Comparator<HashMap<String, String>>() {
            // @Override
            // public int compare(HashMap<String, String> map, HashMap<String, String> map2) {
            // return map.get("simpleName").compareTo(map2.get("simpleName"));
            // }
            // });

            MyLog.i(LOG_TAG, "--- creating custom intent list ---");

            // create the custom intent list
            for (HashMap<String, String> metaInfo : intentMetaInfo) {
                MyLog.i(LOG_TAG, "adding " + metaInfo.get("packageName") + " to the intent list");

                Intent targetedShareIntent = (Intent) prototype.clone();
                targetedShareIntent.setPackage(metaInfo.get("packageName"));
                targetedShareIntent.setClassName(metaInfo.get("packageName"), metaInfo.get("className"));
                targetedShareIntents.add(targetedShareIntent);
            }

            MyLog.i(LOG_TAG, "--- done compiling intent list ---");
            MyLog.i(LOG_TAG, "total count targetedShareIntents: " + targetedShareIntents.size());

            chooserIntent = Intent.createChooser(targetedShareIntents.remove(targetedShareIntents.size() - 1), "Selecteer reis app (1)");
            MyLog.i(LOG_TAG, "--- chooser created ---");
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[] {}));

            MyLog.e(LOG_TAG, "returning filled (custom) chooser");
            return chooserIntent;
        }
    }

    MyLog.e(LOG_TAG, "returning default chooser (empty)");
    return Intent.createChooser(prototype, "Selecteer reis app");
}

Now what happends is that the result chooser shows "No application can perform this action" While the logcat shows there are 5 apps selected.

Logcat Logged results:

06-28 13:04:48.679: I/NavigationTypeActivity(9400): Apps installed on device:356
06-28 13:04:48.687: I/NavigationTypeActivity(9400): --- done compiling list ---
06-28 13:04:48.687: I/NavigationTypeActivity(9400): --- creating custom intent list ---
06-28 13:04:48.687: I/NavigationTypeActivity(9400): adding org.chineseschiphol to the intent list
06-28 13:04:48.687: I/NavigationTypeActivity(9400): adding org.schiphol to the intent list
06-28 13:04:48.687: I/NavigationTypeActivity(9400): adding org.schipholsecurity to the intent list
06-28 13:04:48.687: I/NavigationTypeActivity(9400): adding nl.negentwee to the intent list
06-28 13:04:48.687: I/NavigationTypeActivity(9400): --- done compiling intent list ---
06-28 13:04:48.687: I/NavigationTypeActivity(9400): total count targetedShareIntents: 4
06-28 13:04:48.687: I/NavigationTypeActivity(9400): --- chooser created ---
06-28 13:04:48.687: E/NavigationTypeActivity(9400): returning filled (custom) chooser

解决方案

I got some feedback from an Android guru telling me the chooser should be defined with a working intent, for example the playstore or gmail or something. In theory you could supply the starting intent of your OWN application (you are sure that your own application is installed).

Intent chooserIntent = Intent.createChooser(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=gmail")), "example");

And later, that's why the list removes a single item from the list (you probably don't want your own application in the chooser).