How to Make an Intent with Multiple Actions

How to make an intent with multiple actions

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

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.

EDIT (Complete Solution!):

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

Sample Image

EDIT 2: BUG

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

Android, intent and multiple action

I found the solution here :

Processing more than one button click at Android Widget

use i.setAction("url1...."); (with i your intent)

Thanks

Is it possible to register two actions within one intent-filter for Activity

As the Android documentation states:

When you want to handle multiple kinds of intents, but only in specific combinations of action, data, and category type, then you need to create multiple intent filters.

Otherwise you can group them into one intent-filter.

Android receiver for multiple actions?

I guess you can have multiple s each one having its action element.

<receiver android:name=".myReceiver">
<intent-filter android:priority="1000000">
<action android:name="android.intent.action.ACTION_HEADSET_PLUG" />
</intent-filter>

<intent-filter android:priority="1000000">
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>

And then check the Intent's action in the onReceive of the receiver.

How to use BroadcastReceiver to handle multiple actions?

BroadcastReceiver can handle multiple actions, add them to the intent filter

IntentFilter filter = new IntentFilter();
filter.addAction("action1");
filter.addAction("action2");

then register it with that filter

registerReceiver(mbr, filter);

and inside your own receiver switch for actions

switch(intent.getAction()){
case action1:
// do something
break;
case action2:
// do something
break;
}

AndroidMainfest - should an intent-filter have multiple actions?

Is this correct?

It can be, though in this case I suspect it is not what you want.

This <intent-filter> will match:

  • an Intent with the MAIN action and the LAUNCHER category, or

  • an Intent with the USB_ACCESSORY_ATTACHED and the LAUNCHER category

The former is common. However, I rather doubt that USB_ACCESSORY_ATTACHED will be used with the LAUNCHER category. I am not even sure it is used with activities, as the documentation is a bit muddled on this point.



Related Topics



Leave a reply



Submit