如何用多个动作来制作一个意图 [英] How to make an intent with multiple actions

查看:19
本文介绍了如何用多个动作来制作一个意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示一个活动选择器,显示所有可以查看和/或编辑某些数据的应用程序.有没有一种简单的方法可以做到这一点,或者我是否必须实现我自己的活动选择器对话框?或者也许我可以将 Intent 子类化?谢谢.

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,它会在列表中出现两次,其中一个将打开文件进行查看,另一个用于编辑,您不一定知道哪个是哪个.我认为一个完整的解决方案需要一个自定义应用选择器,正如 Tim 建议的那样.

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_EDIT 应用程序和 ACTION_VIEW 应用程序,我找到了一种方法,通过使用 Tim 提供的代码行将(用于编辑)"字符串附加到其中一个(在我的情况下为 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:错误

如果第一个意图没有找到任何活动,则不会显示任何活动,包括第二个意图找到的任何活动.我最终编写了自己的选择器.我只是在 ExpandableListView 中填充了每种类型意图的标题,并将它们各自的活动作为子项(存储为单独的 LabeledIntent).

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

这篇关于如何用多个动作来制作一个意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆