How to Tell Which App Was Selected by Intent.Createchooser

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

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.

How can I know what app or button user has clicked in the app chooser?

After some digging, I found this answer in here.

This is my updated intent code:

acceptBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

// always initialise app_name to be empty whenever the button is clicked
Constant.APP_NAME = "";

// a “share” Intent
Intent mapIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));

// a “receiver” Intent
Intent receiver = new Intent(getApplicationContext(), AppSelectorReceiver.class);

//one PendingIntent
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, receiver,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);

// Set appChooser Intent
Intent chooser = Intent.createChooser(mapIntent, null, pendingIntent.getIntentSender());

startActivity(chooser);

}
});

in the onResume function, I check whether the user has clicked any application:

@Override
public void onResume(){
super.onResume();

if(!Constant.APP_NAME.isEmpty()){

Log.i(TAG, "User choose: "+ Constant.APP_NAME);

// Proceed to do something here

}

}

And this is my AppSelectorReceiver class:

public class AppSelectorReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

for (String key : Objects.requireNonNull(intent.getExtras()).keySet()) {
try {
ComponentName componentInfo = (ComponentName) intent.getExtras().get(key);
PackageManager packageManager = context.getPackageManager();
assert componentInfo != null;
String appName = (String) packageManager.getApplicationLabel(packageManager.getApplicationInfo(componentInfo.getPackageName(), PackageManager.GET_META_DATA));

Constant.APP_NAME = appName;
}catch (Exception e) {
e.printStackTrace();
}
}
}
}

Get IntentSender object for createChooser method in Android

The chooser target intent is not a PendingIntent. For instance, in the following snippet, I am declaring intent for ACTION_SEND, with type text/plain, and this is the my target intent for the Intent.createChooser. Then I am creating another Intent, receiver, and a handler, the PendingIntent, which will invoke onReceive of my BroadcastReceiver after the user picks something from the chooser.

// Set up the broadcast receiver (preferably as a class member)
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Unregister self right away
context.unregisterReceiver(this);

// Component will hold the package info of the app the user chose
ComponentName component = intent.getParcelableExtra<ComponentName>(Intent.EXTRA_CHOSEN_COMPONENT);
// Do something with component
}
}

Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
intent.setType("text/plain");

// Use custom action only for your app to receive the broadcast
final String shareAction = "com.yourdomain.share.SHARE_ACTION";
Intent receiver = new Intent(shareAction);
receiver.putExtra("test", "test");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, receiver, PendingIntent.FLAG_UPDATE_CURRENT);
Intent chooser = Intent.createChooser(intent, "test", pendingIntent.getIntentSender());

// Before firing chooser activity, register the receiver with our custom action from above so that we receive the chosen app
context.registerReceiver(broadcastReceiver, new IntentFilter(SHARE_ACTION));

startActivity(chooser);

Edit:

The information, in the case of the BroadcastReceiver is embedded in the intent you get as parameter. After you selected one of the option, retrieve the Bundle's extras and using the key Intent.EXTRA_CHOSEN_COMPONENT, you should be able to find what the user picked.

Try adding as simple Log.d to onReceive

for (String key : intent.getExtras().keySet()) {
Log.d(getClass().getSimpleName(), " " + intent.getExtras().get(key));
}

In my example I got

ComponentInfo{org.telegram.messenger/org.telegram.ui.LaunchActivity}

for Telegram and

ComponentInfo{com.google.android.apps.inbox/com.google.android.apps.bigtop.activities.ComposeMessageActivity}

for InBox

Custom filtering of intent chooser based on installed Android package name

The only one additional parameter for the chooser is Intent.EXTRA_INITIAL_INTENTS. Its description is:

A Parcelable[] of Intent or LabeledIntent objects as set with putExtra(String, Parcelable[]) of additional activities to place a the front of the list of choices, when shown to the user with a ACTION_CHOOSER.

I haven't found any way in Android sources to exclude other activities from the list, so it seems there's no way to do what you want to do using the chooser.

EDIT: That's really easy to find out. Just check ChooserActivity and ResolverActivity source code. These classes are rather small.



Related Topics



Leave a reply



Submit