问题
I am using following code to share text
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(Intent.EXTRA_TEXT, "share test");
startActivity(Intent.createChooser(sharingIntent, "Share using"));
I want to know package name of the app chosen by user to share. I have tried doing it using IntentSender for 5.1+ devices using following code
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.putExtra(Intent.EXTRA_TEXT, "share test");
sharingIntent.setType("text/plain");
Intent receiver = new Intent(this, BroadcastTest.class);
receiver.putExtra("test", "test");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, receiver, PendingIntent.FLAG_UPDATE_CURRENT);
Intent chooser = Intent.createChooser(sharingIntent, "Share using", pendingIntent.getIntentSender());
startActivity(chooser);
Following is the BroadcastReceiver
public class BroadcastTest extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
for (String key : intent.getExtras().keySet()) {
Log.d(getClass().getSimpleName(), " " + intent.getExtras().get(key));
}
}
}
Referred this answer https://stackoverflow.com/a/38342788/6053724 but onReceive() of BroadcastReceiver is not getting invoked. Is there anything that I am missing to get it worked?
Update: Found out that above code works well on 5.1(api 22) but on 6.0(api 23) broadcast is not received immediately on picking up app for sharing or sometimes broadcast is lost.
回答1:
I am not sure if this is exactly what you're looking for but there's a lib which might help you with this: https://github.com/zawadz88/material-activity-chooser
It allows you to e.g. handle the clicked app (see https://github.com/zawadz88/material-activity-chooser/blob/master/sample/src/main/java/com/github/zawadz88/sample/TrackingActivityChooserActivity.java)
来源:https://stackoverflow.com/questions/42391497/get-app-package-name-selected-by-user-for-sharing-using-intent-action-send