Search code examples
javaandroidxmleclipseandroid-alarms

how to keep the alarm on after power off?


I am using the following code to set an alarm on a particular date.

What should I add into the android manifest to keep the alarm functional even after power off and after turning off and the on?

    Calendar cal = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
   //cal.add(Calendar.SECOND, 10);

      cal.set(Calendar.DATE,19);  //1-31
    cal.set(Calendar.MONTH,Calendar.DECEMBER);  //first month is 0!!! January is zero!!!
   cal.set(Calendar.YEAR,2012);//year...

    cal.set(Calendar.HOUR_OF_DAY, 16);  //HOUR
    cal.set(Calendar.MINUTE, 39);       //MIN
     cal.set(Calendar.SECOND, 10);       //SEC


       // Create a new PendingIntent and add it to the AlarmManager
    Intent intent = new Intent(MainActivity.this, alarmAct.class);
     PendingIntent pendingIntent = PendingIntent.getService(MainActivity.this, 0,intent, 0);

     //or if you start an Activity
      //PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0,intent, 0);

         AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
      am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);

I have added this to manifest:

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

Does anybody know how to use the RTC_WAKEUP?

:)


Solution

  • What you need to do is register a BroadcastReceiver that uses the action

    <action android:name="android.intent.action.BOOT_COMPLETED" />
    

    in that receiver you start back up the alarm and any service you need