如何使多个动作的意图多个、意图、动作

由网友(孤傲的狼)分享简介:我要显示的活动选择器的显示,可以看到所有的应用程序和/或修改一些数据。有一个简单的方法来做到这一点,或者说我要实现我自己的活动选择对话框?或者,也许我可以继承意图?谢谢你。I want to display an activity chooser that shows all apps that can VIEW a...

我要显示的活动选择器的显示,可以看到所有的应用程序和/或修改一些数据。有一个简单的方法来做到这一点,或者说我要实现我自己的活动选择对话框?或者,也许我可以继承意图?谢谢你。

I want to display an activity chooser that shows all apps that can VIEW and/or EDIT some data. Is there an easy way to do this, or do I have to implement my own activity chooser dialog? Or maybe I can just subclass Intent? Thanks.

推荐答案

我找到了部分解决方案通过使用EXTRA_INITIAL_INTENTS:

I found a partial solution by using EXTRA_INITIAL_INTENTS:

Intent viewIntent = new Intent(Intent.ACTION_VIEW);
Intent editIntent = new Intent(Intent.ACTION_EDIT);
viewIntent.setDataAndType(uri, type);
editIntent.setDataAndType(uri, type);
Intent chooserIntent = Intent.createChooser(editIntent, "Open in...");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { viewIntent });
startActivity(chooserIntent);

我说的部分,因为如果一个应用程序同时支持ACTION_VIEW和ACTION_EDIT它会在列表中显示出来的两倍,其中一个将打开该文件进行查看,另一个用于编辑,而你不一定知道哪个是哪个。我认为,一个完整的解决方案需要自定义的应用程序选择器,蒂姆建议。

I say partial because if an app supports both ACTION_VIEW and ACTION_EDIT it will show up twice in the list, one of which will open the file for viewing and the other for editing, and you wouldn't necessarily know which is which. I think a complete solution would require a custom app chooser, as Tim suggested.

修改(完整的解决方案!):

我发现,不涉及编写自定义应用程序选择器的解决方案。为了从ACTION_VIEW应用程序区分ACTION_EDIT的应用程序,我发现了一种通过使用code蒂姆提供的行追加一个(编辑)字符串为其中的一个标签(在我的情况,ACTION_EDIT)。此外,为了确保在所附字符串未出现被应用程序名称的一部分,我改变它的颜色为青色:

I found a solution that doesn't involving writing a custom app chooser. In order to differentiate ACTION_EDIT apps from ACTION_VIEW apps, I found a way to append a "(for editing)" string to the labels for one of them (in my case, ACTION_EDIT) by using the line of code Tim provided. In addition, to ensure the appended string doesn't appear to be a part of the app name, I changed the color of it to cyan:

PackageManager pm = kyoPrint.getPackageManager();
Intent viewIntent = new Intent(Intent.ACTION_VIEW);
Intent editIntent = new Intent(Intent.ACTION_EDIT);
viewIntent.setDataAndType(uri, type);
editIntent.setDataAndType(uri, type);
Intent openInChooser = Intent.createChooser(viewIntent, "Open in...");

// Append " (for editing)" to applicable apps, otherwise they will show up twice identically
Spannable forEditing = new SpannableString(" (for editing)");
forEditing.setSpan(new ForegroundColorSpan(Color.CYAN), 0, forEditing.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
List<ResolveInfo> resInfo = pm.queryIntentActivities(editIntent, 0);
Intent[] extraIntents = new Intent[resInfo.size()];
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;
    Intent intent = new Intent();
    intent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
    intent.setAction(Intent.ACTION_EDIT);
    intent.setDataAndType(uri, type);
    CharSequence label = TextUtils.concat(ri.loadLabel(pm), forEditing);
    extraIntents[i] = new LabeledIntent(intent, packageName, label, ri.icon);
}

openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
startActivity(openInChooser);

修改2:BUG

如果没有通过第一意图发现活动,编号的活动将被显示,包括任何由第二意图找到。最后我写我自己的选择器。我只是填充的ExpandableListView与标题为每种类型的意图各自的活动,孩子们(个别LabeledIntents存储)。

If there are no activities found by the first intent, NO activities will be displayed, including any found by the second intent. I ended up writing my own chooser. I just populated an ExpandableListView with headings for each type of intent with their respective activities as children (stored as individual LabeledIntents).

阅读全文

相关推荐

最新文章