从 BroadcastReceiver 接收时 Intent 中缺少额外内容
我有一个广播接收器,可以监听所有拨出电话。在另一项活动中,我拨打电话。在我的 BC 中,我希望能够确定活动中创建了哪些调用,因此我在进行调用时使用 putExtras() 来放置标记字段。问题是,在 BC 的 onReceive()
中,我根本看不到额外的数据字段(返回 null)。
这是相关的 Activity 代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
appGlobal gState = (appGlobal)getApplicationContext();
dh = gState.getSqlDataHelper();
Bundle extras = getIntent().getExtras();
if(extras != null)
{
phoneNumber = extras.getString("number");
}
makePhoneCall();
finish();
}
private void makePhoneCall()
{
if (phoneNumber.length() < 1) {
return;
}
String url = "tel:" + phoneNumber;
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
intent.putExtra("number", "bla");
startActivity(intent);
}
这是相关的 BC 代码:
public class CallMeNotServiceCallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if (intent.getStringExtra("number") != null)
{ Log.w("bla", "HAS KEY!!!"); }
...
这种情况需要 PendingIntent 吗?
I have a broadcast receiver which listens for all outgoing calls. In another activity I make an outgoing call. In my BC I want to be able to determine which calls were created in the activity, so I use putExtras()
to place a marker field when I'm making the call. Problem is, in the onReceive()
of the BC I don't see the extra data field at all (returns null).
Here is the relevant Activity code:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
appGlobal gState = (appGlobal)getApplicationContext();
dh = gState.getSqlDataHelper();
Bundle extras = getIntent().getExtras();
if(extras != null)
{
phoneNumber = extras.getString("number");
}
makePhoneCall();
finish();
}
private void makePhoneCall()
{
if (phoneNumber.length() < 1) {
return;
}
String url = "tel:" + phoneNumber;
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
intent.putExtra("number", "bla");
startActivity(intent);
}
And here is the relevant BC code:
public class CallMeNotServiceCallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if (intent.getStringExtra("number") != null)
{ Log.w("bla", "HAS KEY!!!"); }
...
Does this situation require a PendingIntent?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
官方 BroadcastReceiver API 参考 明确指出(第 3 段):
所以杰森和HellBoy 建议,不要在 makePhoneCall() 中启动 Activity,而是向 BroadcastReceiver 发送广播,然后 BroadcastReceiver 启动 Activity(当然,前提是它包含额外的标记)...
The official BroadcastReceiver API reference clearly states (3rd paragraph) :
So as Jason & HellBoy have suggested, instead of starting an Activity in makePhoneCall(), you send a Broadcast to your BroadcastReceiver, which in turn starts an Activity (only if it contains the marker extra of course)...
如果添加额外的日志记录,您是否发现 BroadcastReceiver 实际上根本没有被调用?
使用
sendBroadcast(intent)
发送 BroadcastReceiver 将接收的 Intent。您当前正在使用startActivity
,它期望启动具有特定 Activity 类的 Intent。If you add additional logging, do you find that the BroadcastReceiver actually doesn't get called at all?
Use
sendBroadcast(intent)
to send an Intent that a BroadcastReceiver will receive. You're currently usingstartActivity
which expects an Intent with a particular Activity class to start.