执行createChooser(context,intent,IntentSender)后不调用BroadcastReceiver [英] BroadcastReceiver is not called after createChooser(context,intent,IntentSender) is executed
问题描述
在我向他展示 createChooser()对话框后,我想检测用户选择了哪个应用.因此,我创建了这样的BroadcastReceiver
子类:
I want to detect what app the user selects after I present him with a createChooser() dialog. So I have created my BroadcastReceiver
subclass like this:
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());
}
}
我还已将接收器添加到我的Android 清单文件中:
Also I have added my receiver to my android manifest file:
<application>
...
...
...
<receiver android:name=".helpers.ShareBroadcastReceiver" android:exported="true"/>
</application>
这是调用 createChooser 对话框的代码:
And here is the code that is calling 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);
尽管这是一个明确的PendingIntent
,因为我直接使用ShareBroadcastReceiver
类名,而没有任何intent-filter
,但是在用户单击选择器对话框后,我的广播接收器没有被回调,我在做什么错误吗?
Even though this is an explicit PendingIntent
because I am using the ShareBroadcastReceiver
class name directly without anyintent-filter
, my broadcast receiver is not being callback right after the user clicks on the chooser dialog, what am I doing wrong?
推荐答案
代码中的所有内容都可以.您只需在ShareBroadcastReceiver
的onReceive
方法中更改一行即可捕获Intent的"EXTRA_CHOSEN_COMPONENT"键:
Everything is alright in your code. You only need to change one line in onReceive
method in your ShareBroadcastReceiver
to catch the "EXTRA_CHOSEN_COMPONENT" key of your Intent:
Log.d("INFORMATION", "Received intent after selection: "+intent.getExtras().get(Intent.EXTRA_CHOSEN_COMPONENT));
在您的日志中,您会看到类似以下内容(在我的情况下,我选择了Google Keep):
In your log you will see something like this (in my case I chose Google Keep):
ComponentInfo{com.google.android.keep/com.google.android.keep.activities.ShareReceiverActivity}
这篇关于执行createChooser(context,intent,IntentSender)后不调用BroadcastReceiver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!