BroadcastReceiver 的意图无法正常工作

发布于 2024-12-26 10:07:17 字数 1182 浏览 0 评论 0原文

我怎样才能从额外的意图中“清除”价值观?

Activity:

@Override
public void onCreate(Bundle savedInstanceState) {

...

Intent intent = new Intent(this, MyBroadcastReceiver .class);
intent.putExtra("valueOne", "valOne");
intent.putExtra("valueTwo", true);
intent.putExtra("valueThree", 1);

PendingIntent pendingIntent = PendingIntent.getBroadcast(
    this.getApplicationContext(), 234324243, intent, 0);

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
    + (5 * 1000), pendingIntent);

...

}

BroadcastReceiver:

@Override
public void onReceive(Context context, Intent intent) {

... 

String valueOne= intent.getStringExtra("valueOne");
Boolean valueTwo= intent.getBooleanExtra("valueTwo", false);
Integer valueThree= intent.getIntExtra("valueThree", 0);

// Log.i("info", valueOne) >> valOne
// Log.i("info", valueTwo.toString()) >> false
// Log.i("info", valueThree.toString()) >> 1

...

}

如果我更改 Activity 中的值并再次运行应用程序,我会得到与第一次启动时相同的值。我尝试从手机/虚拟机中删除应用程序,清理项目,但问题仍然存在:(

有人帮助我吗?

How can I 'clean' values from extra intent?

Activity:

@Override
public void onCreate(Bundle savedInstanceState) {

...

Intent intent = new Intent(this, MyBroadcastReceiver .class);
intent.putExtra("valueOne", "valOne");
intent.putExtra("valueTwo", true);
intent.putExtra("valueThree", 1);

PendingIntent pendingIntent = PendingIntent.getBroadcast(
    this.getApplicationContext(), 234324243, intent, 0);

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
    + (5 * 1000), pendingIntent);

...

}

BroadcastReceiver:

@Override
public void onReceive(Context context, Intent intent) {

... 

String valueOne= intent.getStringExtra("valueOne");
Boolean valueTwo= intent.getBooleanExtra("valueTwo", false);
Integer valueThree= intent.getIntExtra("valueThree", 0);

// Log.i("info", valueOne) >> valOne
// Log.i("info", valueTwo.toString()) >> false
// Log.i("info", valueThree.toString()) >> 1

...

}

If I change value in Activity and run application again, I get same values like in first start. I try delete app from my phone/virtual machine, clean project, but problem stay :(

Anybody help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

往事随风而去 2025-01-02 10:07:17

在待处理意图声明中尝试设置以下标志:

PendingIntent.FLAG_UPDATE_CURRENT

在您的情况下,它应该如下:

PendingIntent pendingIntent = PendingIntent.getBroadcast(
    this.getApplicationContext(), 234324243, intent, PendingIntent.FLAG_UPDATE_CURRENT);

In the Pending Intent declaration try to set the following flag:

PendingIntent.FLAG_UPDATE_CURRENT

In your case it should be the following:

PendingIntent pendingIntent = PendingIntent.getBroadcast(
    this.getApplicationContext(), 234324243, intent, PendingIntent.FLAG_UPDATE_CURRENT);
梦初启 2025-01-02 10:07:17

首先,我建议您在这里格式化代码。这样这里的人们就会很高兴阅读您的问题代码。上面的代码列表不会向系统注册任何 BroadcastReceiver。您最好查看 ApiDemo 了解更多详细信息。

另请参阅这个

First, i suggest you to format your code here. So that people here will be glad to read your problem code. Your code list above does NOT register any BroadcastReceiver to the system. You'd better to check out ApiDemo for more details.

And Also See this one

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