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);
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 theMAIN
action and theLAUNCHER
category, oran
Intent
with theUSB_ACCESSORY_ATTACHED
and theLAUNCHER
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
Java.Lang.Illegalstateexception: Fragment Not Attached to Activity
How to Embed a Custom Font in an Android Application
Android - Swipe to Delete Recyclerview
Difference Between Build.Gradle (Project) and Build.Gradle (Module)
Insertion of Thousands of Contact Entries Using Applybatch Is Slow
How to Programmatically Add a Submenu Item to the New Material Design Android Support Library
How to Iterate Through a View's Elements
Error:(9, 5) Error: Resource Android:Attr/Dialogcornerradius Not Found
Android Spinner with Different Layouts for "Drop Down State" and "Closed State"
How to Add Button in Actionbar(Android)
Create SQLite Database in Android
Running Task Periodicaly(Once a Day/Once a Week)
Illegalstateexception: Can't Change Container Id of Fragment
Developing a Custom Lock Screen
How Does Doze Mode Affect Background/Foreground Services, With/Without Partial/Full Wakelocks
Setrotation(90) to Take Picture in Portrait Mode Does Not Work on Samsung Devices