简体   繁体   English

BroadcastReceiver 的 onReceive 中的 Intent extras 为空

[英]Intent extras are empty in onReceive of BroadcastReceiver

I want to get some arguments from Intent in onReceive() method of BroadcastReceiver class.我想BroadcastReceiver 类的onReceive()方法中的Intent 获取一些参数 But there's only int ALARM_COUNT = 1, although I put two args: my Parcelable Alarm object and test int (for the case when there is some problem with alarm object).但是只有 int ALARM_COUNT = 1,虽然我放了两个参数:我的 Parcelable Alarm 对象和 test int(对于警报对象有问题的情况)。

I set alarm like this:我这样设置闹钟:

private void setCurrentAlarm(Alarm alarm) {
        long time = System.currentTimeMillis() + getClosestTimeDifference(alarm);

        mAlarmManager.set(AlarmManager.RTC_WAKEUP, time, createPendingIntent(alarm));
    }

There is how I create PendingIntent variable:我是如何创建 PendingIntent 变量的:

private PendingIntent createPendingIntent(@NonNull Alarm alarm) {
        Intent intent = new Intent(mContext, AlarmBroadcastReceiver.class);

        intent.putExtra(KEY_ALARM, alarm);
        intent.putExtra("TEST", 1010120012);

        return PendingIntent.getBroadcast(mContext, alarm.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
    }

And onReceive() method in my AlarmBroadcaseReceiver class:我的 AlarmBroadcaseReceiver 类中的 onReceive() 方法:

    @Override
    public void onReceive(Context context, Intent intent) {
           Alarm receivedAlarm = intent.getParcelableExtra(KEY_ALARM); //is always null
           int receivedInt = intent.getIntExtra("TEST", -1); //the same empty, -1
    }

As you can see Intent contains only some ALARM_COUNT extra, but there aren't my extras.如您所见,Intent 仅包含一些额外的 ALARM_COUNT,但没有我的额外内容。

What to do?怎么办? How can I get them?我怎样才能得到它们?

Hi Denis you can send the value like a String and after do parse to int.嗨,丹尼斯,您可以像字符串一样发送值,然后解析为 int。

private PendingIntent createPendingIntent(@NonNull Alarm alarm) {
    Intent intent = new Intent(mContext, AlarmBroadcastReceiver.class);

    intent.putExtra(KEY_ALARM, alarm);
    intent.putExtra("TEST", "1010120012");

    return PendingIntent.getBroadcast(mContext, alarm.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

@Override
public void onReceive(Context context, Intent intent) {
       Alarm receivedAlarm = intent.getParcelableExtra(KEY_ALARM); //is always null
       Bundle extras = intent.getExtras();
       String testString = (String) extras.get("TEST");
       int test = Integer.parseInt(testString)
}

And you will have the value of "TEST" in your method onReceive你将在你的方法 onReceive 中拥有“TEST”的价值

Using flags as shown in the examples below was helpful for me.使用下面示例中所示的标志对我很有帮助。

retrun PendingIntent.getBroadcast(mContext, alarm.getId(), intent, PendingIntent.FLAG_CANCEL_CURRENT)
retrun PendingIntent.getBroadcast(mContext, alarm.getId(), intent, PendingIntent.FLAG_IMMUTABLE)
retrun PendingIntent.getBroadcast(mContext, alarm.getId(), intent, PendingIntent.FLAG_ONE_SHOT)
retrun PendingIntent.getBroadcast(mContext, alarm.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM