Search code examples
androidalarmmanager

Alarm manager in android


I have created simple alarm app. It works fine but, if i change time of device manually then my alarm doesn't work....

This is my code:

    Calendar = Calendar.getInstance();
    //c.add(Calendar.DAY_OF_WEEK,1);
    c.set(Calendar.HOUR_OF_DAY,15);
    c.set(Calendar.MINUTE, 00);
    c.set(Calendar.SECOND, 00);
    c.set(Calendar.MILLISECOND, 0);

    Intent intent = new Intent(HomeActivity.this, MyAlarmService.class);
    pendingIntent = PendingIntent.getService(HomeActivity.this, 0, intent, 0);
    alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, c.getTimeInMillis(), pendingIntent);

Is there any suggestion?


Solution

  • [Adding the answer from comment to here]

    You need to add a receiver to set the alarm again..on date and time zone change then alarm got reset so use this

    <receiver android:name=".AlarmInitReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.intent.action.TIME_SET" /> <action android:name="android.intent.action.TIMEZONE_CHANGED" /> 
    </intent-filter> 
    </receiver>