用户更改系统时间时会启动AlarmManager警报吗? [英] AlarmManager alarm start when system time is changed by user?
问题描述
我正在使用 AlarmManager 类来设置正常运行的警报。
i am using AlarmManager class for setting Alarms it is working fine.
但是,如果我将闹铃设置为9pm,而当前时间是8pm,并且我将系统时间更改为10pm
,则闹铃9pm会自动启动。因此,要解决此问题
But if i set alarm like 9pm and current time is 8pm and i changed the system time to 10pm
then alarm 9pm alarm start automatically. so to solve this issue
我进行了很多搜索,但未找到任何好的答案
请帮助
这是我的警报设置代码
final int id = (int) System.currentTimeMillis();
Intent intent = new Intent(this, AlarmReceiver.class);
intent.putExtra("requestCode", id);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 2*60*1000, pendingIntent);
推荐答案
其中一个选项是将所有设置的警报存储在数据库,然后创建一个BroadcastReceiver,它将监听 ACTION_TIME_CHANGE
动作。当用户更改时间时,它将被触发。然后创建一个 IntentService
,它将负责重置警报。在此服务类中:
One of the options is to store all set alarms in database, then create a BroadcastReceiver which will listen for ACTION_TIME_CHANGE
action. When user changes time it will be triggered. Then create a IntentService
which will be responsible for resetting alarms. In this service class:
- 读取数据库并识别所有已通过的警报。
- 取消已通过的警报
- 设置第二天的警报
您的代码可能如下所示:
Your code may look like as below:
在您的清单中:
<uses-permission android:name="android.permission.ACTION_TIME_CHANGE"/>
及以下活动:
<receiver android:name=".TimeChangedReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.TIME_SET" />
</intent-filter>
</receiver>
<service android:name=".RestartAlarmsService"/>
在其中创建类 TimeChangedReceiver:
Create class "TimeChangedReceiver" inside of which:
public class TimeChangedReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if("android.intent.action.TIME_SET".equals(intent.getAction())) {
Intent i = new Intent(context, RestartAlarmsService.class);
ComponentName service = context.startService(i);
}
}
}
创建 RestartAlarmsService类,其中:
Create "RestartAlarmsService" class inside of which:
public class RestartAlarmsService extends IntentService {
public RestartAlarmsService() {
super("RestartAlarmsService");
}
@Override
protected void onHandleIntent(Intent intent) {
// read db here
// then cancel passed alarms
// reset them to next day
}
}
您可以找到许多有关如何使用数据库以及如何在代码中实现它的教程。希望我的回答对您有所帮助。
You can find many tutorials on how to use Databases and implement it in your code. Hope my answer is somehow helpful.
这篇关于用户更改系统时间时会启动AlarmManager警报吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!