Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell which app was selected by Intent.createChooser?

Code:

Intent launchIntent = new Intent(Intent.ACTION_MAIN); launchIntent.addCategory(Intent.CATEGORY_HOME); Intent chooser = Intent.createChooser(launchIntent, "Complete Action using.."); activity.startActivity(chooser); 

I don't see any way to tell which Intent (HOME category launcher) was selected. There is no Intent.addOnActionSetListener, and no chooser.addOnIntentChosenListener etc.

So how can I tell which was selected? Do I have to write my own chooser for this?

like image 784
nagylzs Avatar asked Aug 25 '15 11:08

nagylzs


People also ask

What is the Intent action to launch phone application with number?

Intent callIntent = new Intent(Intent. ACTION_DIAL, number); When your app invokes this intent by calling startActivity() , the Phone app initiates a call to the given phone number.

How do I send Intent from one app to another?

When you construct an intent, you must specify the action you want the intent to perform. Android uses the action ACTION_SEND to send data from one activity to another, even across process boundaries. You need to specify the data and its type.

What is setType in Intent Android?

setType(String mimeType) input param is represent the MIME type data that u want to get in return from firing intent(here myIntent instance). by using one of following MIME type you can force user to pick option which you desire. Please take a Note here, All MIME types in android are in lowercase.


2 Answers

On Android 5.1+, you can use the three-parameter edition of the createChooser() method, where the last parameter is an IntentSender that you can use to find out what was chosen.

Prior to Android 5.1, there is nothing in Android to let you know what the user chose.

like image 73
CommonsWare Avatar answered Sep 22 '22 16:09

CommonsWare


The answer provided by BinHe works but the problem is that a big number of apps is shown. In this solution I use the Intent.ACTION_PICK_ACTIVITY but only the apps compatible with Intent.ACTION_SEND will be shown, and you will know which option the user selected.

public void doSocialShare(String title, String text, String url){     // First search for compatible apps with sharing (Intent.ACTION_SEND)     List<Intent> targetedShareIntents = new ArrayList<Intent>();     Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);     shareIntent.setType("text/plain");     // Set title and text to share when the user selects an option.     shareIntent.putExtra(Intent.EXTRA_TITLE, title);     shareIntent.putExtra(Intent.EXTRA_TEXT, url);     shareIntent.putExtra(Intent.EXTRA_TEXT, text);     List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(shareIntent, 0);     if (!resInfo.isEmpty()) {         for (ResolveInfo info : resInfo) {             Intent targetedShare = new Intent(android.content.Intent.ACTION_SEND);             targetedShare.setType("text/plain"); // put here your mime type             targetedShare.setPackage(info.activityInfo.packageName.toLowerCase());             targetedShareIntents.add(targetedShare);         }         // Then show the ACTION_PICK_ACTIVITY to let the user select it         Intent intentPick = new Intent();         intentPick.setAction(Intent.ACTION_PICK_ACTIVITY);         // Set the title of the dialog         intentPick.putExtra(Intent.EXTRA_TITLE, title);         intentPick.putExtra(Intent.EXTRA_INTENT, shareIntent);         intentPick.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray());         // Call StartActivityForResult so we can get the app name selected by the user         this.startActivityForResult(intentPick, REQUEST_CODE_MY_PICK);     } } 

Finally, to be able to get the app selected by the user you must override the onActivityResult on your activity:

@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {     super.onActivityResult(requestCode, resultCode, data);     if(requestCode == REQUEST_CODE_MY_PICK) {         if(data != null && data.getComponent() != null && !TextUtils.isEmpty(data.getComponent().flattenToShortString()) ) {             String appName = data.getComponent().flattenToShortString();             // Now you know the app being picked.             // data is a copy of your launchIntent with this important extra info added.              // Start the selected activity             startActivity(data);         }     }  } 
like image 28
JUANM Avatar answered Sep 22 '22 16:09

JUANM