Alarmmanager在重启手机时死机。如何使用相同的值重新启动计时器

时间:2013-11-28 04:26:44

标签: android alarmmanager

我有一个警报管理器,我想在安装应用程序后设置它,并在每个特定时间重复警报,目前当我重新启动手机警报死机时会发生什么。任何想法如何让它一杆气走。我也在寻找长时间的警报说2周。如何将该值添加到警报而不是。

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

以下是我的代码:

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

        }

}

我正在呼叫的地方://如果我在onCreate上启动,它将在onCreate期间每次重置计时器,我不想要。

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

1 个答案:

答案 0 :(得分:2)

为了每两周重复一次警报,请尝试以下代码:

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

另请查看this解决方案。

您可以询问是否有任何进一步的疑问:)