从 BroadcastReceiver 接收时 Intent 中缺少额外内容

发布于 2024-10-16 01:15:23 字数 1306 浏览 6 评论 0原文

我有一个广播接收器,可以监听所有拨出电话。在另一项活动中,我拨打电话。在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

土豪 2024-10-23 01:15:23

官方 BroadcastReceiver API 参考 明确指出(第 3 段):

[...] Intent 广播机制与用于启动 Activity 的 Intent 完全分开。 BroadcastReceiver 无法查看或捕获与 startActivity() 一起使用的 Intent; [...]

所以杰森和HellBoy 建议,不要在 makePhoneCall() 中启动 Activity,而是向 BroadcastReceiver 发送广播,然后 BroadcastReceiver 启动 Activity(当然,前提是它包含额外的标记)...

The official BroadcastReceiver API reference clearly states (3rd paragraph) :

[...] the Intent broadcast mechanism is completely separate from Intents that are used to start Activities. There is no way for a BroadcastReceiver to see or capture Intents used with startActivity(); [...]

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)...

红尘作伴 2024-10-23 01:15:23

如果添加额外的日志记录,您是否发现 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 using startActivity which expects an Intent with a particular Activity class to start.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文