意图额外内容似乎没有刷新 [英] Intent extras don't seem to refresh

查看:114
本文介绍了意图额外内容似乎没有刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我按下按钮时,它调用一个方法,该方法获取用户输入并将其设置为变量,然后将该变量放入一个意图之外的变量中,并发送到BroadcastReceiver,后者根据该值决定要做什么.它仅在第一次使用时有效,在接下来的所有印刷机中,无论​​可变值是否更改,都不会改变额外的值.它永远保持第一次按下时设置的值.如何用新值额外重写意图?

When I press the button it calls a method which gets user input and sets it to a variable, then that variable is put to an intent extra and sent to BroadcastReceiver which decides what to do depending on that value. It only works the first time, on all the next presses intent extra value isn't changed no matter if that variable value changes. It forever holds the value which is set at the first press. How to rewrite intent extra with a new value?

我如何设置额外费用:

Intent intentForReceiver = new Intent(this, AlarmReceiver.class);
intentForReceiver.putExtra("checkBox1", checkBox1.isChecked());
        PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0,
intentForReceiver, 0);

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
    AlarmManager.INTERVAL_DAY, alarmIntent);

我如何在BroadcastReceiver上接收额外的款项并将其用于服务目的:

How I receive the extra on BroadcastReceiver and put it to the intent for service:

Intent intentForService = new Intent(context, CMService.class);
intent.putExtra("checkBox1", intentForReceiver.getExtras().getBoolean("checkBox1"));
context.startService(intentForService);

我如何从服务中获得额外收益:

How I receive the extra on Service:

if(intentForService.getExtras().getBoolean("checkBox1")) {
...
} else {
...
}

这是我的代码.如果我取消选中checkBox1并按下按钮,它仍然会执行仅当checkBox1.isChecked()为true时才应执行的工作,因此这意味着该值在第一次按下后将永远保持为true,并且当我取消选中checkBox1.isChecked()时不会更新. checkBox1.

So this is my code. If I uncheck the checkBox1 and press the button, it still does the job which should be done only if checkBox1.isChecked() is true, so it means this value stays true forever after the first press and doesn't update when I uncheck the checkBox1.

推荐答案

在重新创建警报之前,请取消第一个警报.为此,您需要重新创建Intent并在AlarmManager上调用cancel.例如,我在Alarm类中有一个方法,可以在开始新的方法之前为我取消该方法

Before recreating your alarm, cancel the first one. To do this, you recreate your Intent and call cancel on the AlarmManager. For instance, I have a method in an Alarm class that cancels it for me before starting a new one

// cancel existing logout alarm
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 
     REQUEST_CODE, alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT);     
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.cancel(pendingIntent);

然后重新创建PendingIntent并设置新的AlarmManager

Then recreate the PendingIntent and set the new AlarmManager

此答案进一步说明了

这篇关于意图额外内容似乎没有刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆