gpt4 book ai didi

android - 无法从 Intent 中获取额外的值

转载 作者:行者123 更新时间:2023-11-29 17:59:01 26 4
gpt4 key购买 nike

我知道我可以 intent.putExtra(key, value) 来放置值,我们可以 intent.getStringExtra(key) 来检索值,但是在下面的代码中它没有按预期运行。检索值时,我收到 NUllPointer,printLn 需要一条消息

我设置值的代码:

public void SetAlarm(Context context, int seconds, String type) {
CustomLog.logBlue(Thread.currentThread().getStackTrace(), "set alarm");

AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
intent.putExtra(ONE_TIME, Boolean.FALSE); //ONE_TIME = "oneTime"
intent.putExtra("c", "ayush");

PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), seconds * 1000, pi);

CustomNotification.generateNotification(1, context, "Tracking enabled");
}

我正在 BroadcaseReceiver 的 onReceive 中检索值:[省略无关代码]

@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
Log.e("test", extras.getString("c"); <------ ERROR
// also tried intent.getStringExtra("c");
}

最佳答案

使用标志 FLAG_UPDATE_CURRENT打电话时 PendingIntent.getBroadcast() .在您的代码中:

PendingIntent pi = PendingIntent.getBroadcast(
context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);

关于android - 无法从 Intent 中获取额外的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17313064/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com