I want to determine which application the user selects after I present the createChooser () dialog to him. So I created my subclass of BroadcastReceiver as follows:
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class ShareBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.d("INFORMATION", "Received intent after selection: "+intent.getExtras().toString()); } }
I also added my receiver to the android manifest file:
<application> ... ... ... <receiver android:name=".helpers.ShareBroadcastReceiver" android:exported="true"/> </application>
And here is the code that calls the createChooser dialog:
Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND); sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); sendIntent.setType("image/png"); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) { Log.d("INFORMATION", "The current android version allow us to know what app is chosen by the user."); Intent receiverIntent = new Intent(this,ShareBroadcastReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, receiverIntent, PendingIntent.FLAG_CANCEL_CURRENT); sendIntent = Intent.createChooser(sendIntent,"Share via...", pendingIntent.getIntentSender()); } startActivity(sendIntent);
Even if it is an explicit PendingIntent , because I use the ShareBroadcastReceiver class ShareBroadcastReceiver directly without intent-filter , my broadcast receiver is not a callback right after the user clicks on the selection dialog, what am I doing wrong?
source share