AlarmManager alarm start when system time is changed by user?

爷,独闯天下 提交于 2019-12-12 01:42:13

问题


i am using AlarmManager class for setting Alarms it is working fine.

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

i have searched so much but did not found any good answer Please help

here is my code for alarm setting

    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);

回答1:


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:

  1. Read db and identify all passed alarms.
  2. Cancel passed alarms
  3. Set alarms for next day

Your code may look like as below:

In your Manifest:

<uses-permission android:name="android.permission.ACTION_TIME_CHANGE"/>

and below activities:

 <receiver android:name=".TimeChangedReceiver" android:enabled="true">
     <intent-filter>
         <action android:name="android.intent.action.TIME_SET" />
     </intent-filter>
 </receiver>

 <service android:name=".RestartAlarmsService"/>

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);
        }

    }
}

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.




回答2:


yes it will give you broadcast, as your pending intent object is still attached to that event while you change time that is greater than you alarm firing time.

solution- validate your condition while you receive broadcast from alarm manager



来源:https://stackoverflow.com/questions/39527582/alarmmanager-alarm-start-when-system-time-is-changed-by-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!