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.