Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Chosen App from Intent.createChooser

I am trying to capture the result of Intent.createChooser to know which app a user selected for sharing.

I know there have been a lot of posts related to this:

  • How to know which application the user chose when using an intent chooser?
  • https://stackoverflow.com/questions/6137592/how-to-know-the-action-choosed-in-a-intent-createchooser?rq=1
  • How to get the user selection from startActivityForResult(Intent.createChooser(fileIntent, "Open file using..."), APP_PICKED);?
  • Capturing and intercepting ACTION_SEND intents on Android

but these posts are somewhat old, and I am hoping that there might be some new developments.

I am trying to implement a share action without having it be present in the menu. The closest solution to what I want is provided by ClickClickClack who suggest implementing a custom app chooser, but that seems heavy handed. Plus, it seems like there might be some Android hooks to get the chosen app, like the ActivityChooserModel.OnChooseActivityListener.

I have the following code in my MainActivity, but the onShareTargetSelected method is never getting called.

    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_TEXT, shareMessage());
    sendIntent.setType("text/plain");

    Intent intent = Intent.createChooser(sendIntent, getResources().getText(R.string.share_prompt));

    ShareActionProvider sap = new ShareActionProvider(this);
    sap.setShareIntent(sendIntent);
    sap.setOnShareTargetSelectedListener(new ShareActionProvider.OnShareTargetSelectedListener() {
        @Override
        public boolean onShareTargetSelected(ShareActionProvider source, Intent intent) {
            System.out.println("Success!!");
            return false;
        }
    });

    startActivityForResult(intent, 1);
like image 709
dsg Avatar asked Jul 03 '14 19:07

dsg


2 Answers

As of API level 22 it is now actually possible. In Android 5.1 a method (createChooser (Intent target, CharSequence title, IntentSender sender)) was added that allows for receiving the results of the user's choice. When you provide an IntentSender to createChooser, the sender will be notified by the chooser dialog with the ComponentName chosen by the user. It will be supplied in the extra named EXTRA_CHOSEN_COMPONENT int the IntentSender that is notified.

like image 145
Jade Avatar answered Oct 19 '22 09:10

Jade


I am trying to capture the result of Intent.createChooser to know which app a user selected for sharing.

That is not possible.

Other "choosing" solutions, like ShareActionProvider, may offer more. I have not examined the Intent handed to onShareTargetSelected() to see if it contains the ComponentName of the chosen target, though the docs suggest that it should.

And, if for some reason it does not, you are welcome to try to fork ShareActionProvider to add the hooks you want.

The reason why createChooser() cannot be handled this way is simply because the "choosing" is being done by a separate process from yours.

I have the following code in my MainActivity, but the onShareTargetSelected method is never getting called.

ShareActionProvider goes in the action bar. You cannot just create an instance, call a couple of setters, and expect something to happen.

like image 38
CommonsWare Avatar answered Oct 19 '22 08:10

CommonsWare