/ / BroadcastReceiver nie jest wywoływany po wykonaniu createChooser (kontekst, zamiar, IntentSender) - Android, Android-pendingintent, Android-broadcastreceiver, Android-Intent-Chooser

BroadcastReceiver nie jest wywoływany po wykonaniu createChooser (context, intent, IntentSender) - Android, Android-w toku, Android-odbiornik, Android-intent-Chooser

Chcę wykryć, którą aplikację wybiera użytkownik po tym, jak mu go przedstawię createChooser () dialog. Więc stworzyłem mój BroadcastReceiver taka podklasa:

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());
}
}

Dodałem również mój odbiornik do mojego Androida oczywisty plik:

<application>
...
...
...
<receiver android:name=".helpers.ShareBroadcastReceiver" android:exported="true"/>
</application>

A oto kod, który wywołuje 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);

Chociaż jest to wyraźne PendingIntent ponieważ używam ShareBroadcastReceiver nazwa klasy bezpośrednio bez żadnychintent-filter, mój odbiornik nie oddzwania od razu po kliknięciu przez użytkownika okna dialogowego selektora, co robię źle?

Odpowiedzi:

3 dla odpowiedzi № 1

Wszystko jest w porządku w twoim kodzie. Musisz zmienić tylko jedną linię onReceive metoda w twoim ShareBroadcastReceiver aby złapać klucz „EXTRA_CHOSEN_COMPONENT” swojego zamiaru:

Log.d("INFORMATION", "Received intent after selection: "+intent.getExtras().get(Intent.EXTRA_CHOSEN_COMPONENT));

W swoim dzienniku zobaczysz coś takiego (w moim przypadku wybrałem Google Keep):

ComponentInfo{com.google.android.keep/com.google.android.keep.activities.ShareReceiverActivity}