我想在我用createChooser()对话框呈现他之后检测用户选择的应用程序.所以我创建了我的broadcastReceiver子类,如下所示:
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清单文件中:
lication>.........
这是调用createChooser对话框的代码:
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,receiverIntent,PendingIntent.FLAG_CANCEL_CURRENT); sendIntent = Intent.createChooser(sendIntent,"Share via...",pendingIntent.getIntentSender());}startActivity(sendIntent);
即使这是一个显式的PendingIntent,因为我直接使用SharebroadcastReceiver类名而没有任何内联过滤器,我的广播接收器在用户点击选择器对话框后没有立即回调,我做错了什么?最佳答案代码中的一切都没问题.您只需要在SharebroadcastReceiver中的onReceive方法中更改一行,以捕获Intent的“EXTRA_CHOSEN_COMPONENT”键:
Log.d(“informatION”,“选择后收到的意图:”intent.getExtras().get(Intent.EXTRA_CHOSEN_COMPONENT));
在您的日志中,您将看到类似的内容(在我的情况下,我选择了Google Keep):
ComponentInfo{com.Google.androID.keep/com.Google.androID.keep.activitIEs.ShareReceiverActivity}
总结 以上是内存溢出为你收集整理的android – 执行createChooser(context,intent,IntentSender)后不调用BroadcastReceiver全部内容,希望文章能够帮你解决android – 执行createChooser(context,intent,IntentSender)后不调用BroadcastReceiver所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)