user45678
user45678

Reputation: 1514

Alarmmanager dies during restart of phone. How to restart timer again with same value

I have an alarm manager which i want to set once the application is installed and the repeat alarm every particular time, currently what happens when i restart the phone the alarm dies. Any idea how to make it one shot go. Also I was looking for long duration alarm say 2week. How can i add that value to my alarm instead of.

//After after 10 seconds
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 10 , pi); 

Below is my code :

LogListAlarmBroadcast.java

public class LogListAlarmBroadcast extends BroadcastReceiver {

    final public static String ONE_TIME = "onetime";

     @Override
     public void onReceive(Context context, Intent intent) {
       PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
             PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG");
             //Acquire the lock
             wl.acquire();

             //You can do the processing here.
             Bundle extras = intent.getExtras();
             StringBuilder msgStr = new StringBuilder();

             if(extras != null && extras.getBoolean(ONE_TIME, Boolean.FALSE)){
              //Make sure this intent has been sent by the one-time timer button.
              msgStr.append("One time Timer : ");
             }
             Format formatter = new SimpleDateFormat("hh:mm:ss a");
             msgStr.append(formatter.format(new Date()));

             Toast.makeText(context, msgStr, Toast.LENGTH_LONG).show();
             Log.d("TIMERLOG", "Timer Log : " + msgStr);

             //Release the lock
             wl.release();
     }

    public void CancelAlarm(Context context)
        {
            Intent intent = new Intent(context, LogListAlarmBroadcast.class);
            PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
            AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            alarmManager.cancel(sender);
        }

        public void setOnetimeTimer(Context context){
         AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
            Intent intent = new Intent(context, LogListAlarmBroadcast.class);
            intent.putExtra(ONE_TIME, Boolean.TRUE);
            PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
            am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pi);
        }

        public void SetAlarm(Context context) {
            // TODO Auto-generated method stub
            AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
            Intent intent = new Intent(context, LogListAlarmBroadcast.class);
            intent.putExtra(ONE_TIME, Boolean.FALSE);
            PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
            //After after 10 seconds
            am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 10 , pi); 

        }

}

Place where I am calling : //Here if i initiate on onCreate it will reset timer every time during onCreate which i don't want.

public class LogListView extends ListActivity {

    private LogListAlarmBroadcast alarm;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        context = this;
        alarm = new LogListAlarmBroadcast();
        //startRepeatingTimer();

    }

    @Override
    protected void onStart() {
      super.onStart();
    }

    public void startRepeatingTimer() {
         Context context = this.getApplicationContext();
         if(alarm != null){
          alarm.SetAlarm(context);
          Log.d("TIMERLOG", "Timer onCreate");
          Toast.makeText(context, "Alarm Repeated", Toast.LENGTH_SHORT).show();
         } else{
          Log.d("TIMERLOG", "Timer didn't come onCreate");
          Toast.makeText(context, "Alarm is null", Toast.LENGTH_SHORT).show();
         }
    }
}

Upvotes: 0

Views: 333

Answers (1)

Jigar Pandya
Jigar Pandya

Reputation: 2147

In order to repeat the alarm every two weeks try below code :

 alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,time_in_milliseconds,alarmManager.INTERVAL_DAY * 14,pendingIntent);

Also look this solution.

you can ask if you have any further queries :)

Upvotes: 2

Related Questions