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?
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.
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.
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.
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.
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); } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With