Java – from intent Createchooser gets the selected application
I tried to capture intent The results of createchooser to understand the application that users choose to share
I know there are many 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 a little old. I hope there may be some new developments
I'm trying to implement share action without appearing in the menu The closest solution I want is provided by clickclickclark, who recommends implementing a custom application selector, but this seems important In addition, it seems that there may be some Android hooks to get the selected application, such as activitychoosermodel OnChooseActivityListener.
I have the following code in mainactivity, but the onsharetargetselected method will never be 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);
Solution
Starting with API level 22, it is now actually possible In Android 5.1, a method (createchooser (intent target, charsequence title, intent sender sender)) is added to allow receiving the results selected by the user When you provide intentsender to create a selector, the selector dialog box will notify the sender through the user selected componentname It will be extra named extra in the notified intentsender_ CHOSEN_ Provided by component
