0

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();
         }
    }
}
user45678
  • 1,504
  • 6
  • 29
  • 58
  • if its a service u cud have start_sticky defined.. in the case of a broadcast receiver u cud have sticky broadcast i believe. And also permission of on boot .. – Rat-a-tat-a-tat Ratatouille Nov 28 '13 at 04:28
  • possible duplicate of [Alarm Manager Example](http://stackoverflow.com/questions/4459058/alarm-manager-example) – FoamyGuy Nov 28 '13 at 04:31
  • 1
    You have to use a BroadcastReciever to listen for the boot event, and when it happens "manually" re-set your alarm. Note that this means you'll also need to store the data related to your alarm in some persistent fashion as well if you are not. [Here are](http://stackoverflow.com/questions/16396715/resetting-alarms-on-reboot) two [examples](http://stackoverflow.com/questions/17941371/repeating-alarm-manager-after-reboot) Also see the link in my other comment for more detail over the whole process. – FoamyGuy Nov 28 '13 at 04:34
  • @FoamyGuy : Is there anyway to save timestamp before reboot and start alarm with exactly same time onboot ? Can we do that ? – user45678 Nov 28 '13 at 04:56
  • @user45678: yes,you can use `SharedPreferences` to save that. – Mehul Joisar Nov 28 '13 at 05:35
  • You can save the time and date values in shared preference or in sqlite database and fetch it when you restart the device and again set the alarm with those values. – Jigar Pandya Nov 28 '13 at 05:43
  • Thanks @JIGARPANDYA. Can you tell me the event fired before phone goes into off mode ? – user45678 Nov 28 '13 at 06:19
  • 1
    @user45678 `` and for HTC devices the previous one plus this `` – d3m0li5h3r Nov 28 '13 at 06:21
  • 1
    @user45678 : Save the time and date values when you set the alarm in database or some persistent storage.and when phone reboots,get those values...you have to add following code to perform some task when phone starts – Jigar Pandya Nov 28 '13 at 06:25

1 Answers1

2

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

Community
  • 1
  • 1
Jigar Pandya
  • 2,129
  • 2
  • 17
  • 27