gpt4 book ai didi

android - 空 Intent 选择器(没有应用程序可以执行此操作)

转载 作者:太空宇宙 更新时间:2023-11-03 10:49:01 24 4
gpt4 key购买 nike

我的 Intent 选择器是基于白名单的(只有一部分应用程序会显示在 Intent 选择器中)。该代码基于另一个相反的代码;黑名单应用程序。我从 here 得到了那个代码和 this是与之相关的相关讨论。

如何创建选择器的上下文:

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

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

// 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");
}

现在发生的事情是结果选择器显示“没有应用程序可以执行此操作”,而 logcat 显示已选择 5 个应用程序。

Logcat 记录的结果:

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

最佳答案

我从一位 Android 大师那里得到了一些反馈,告诉我应该使用工作 Intent 来定义选择器,例如 playstore 或 gmail 或其他东西。理论上,您可以提供您自己的应用程序的启动 Intent (您确定您自己的应用程序已安装)。

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

后来,这就是列表从列表中删除单个项目的原因(您可能不希望选择器中有您自己的应用程序)。

关于android - 空 Intent 选择器(没有应用程序可以执行此操作),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17363704/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com