Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Alarmmanager在重新启动手机时死亡。如何使用相同的值重新启动计时器_Android_Alarmmanager - Fatal编程技术网

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

Android Alarmmanager在重新启动手机时死亡。如何使用相同的值重新启动计时器,android,alarmmanager,Android,Alarmmanager,我有一个报警管理器,我想设置一旦应用程序安装和重复报警每一个特定的时间,目前会发生什么,当我重新启动手机时,报警死亡。你知道怎么让它一次成功吗。我也在寻找长时间报警,比如说2周。如何将该值添加到闹钟中,而不是添加到闹钟中 //After after 10 seconds am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 10 , pi); 下面是我的代码: LogListAlarmBroad

我有一个报警管理器,我想设置一旦应用程序安装和重复报警每一个特定的时间,目前会发生什么,当我重新启动手机时,报警死亡。你知道怎么让它一次成功吗。我也在寻找长时间报警,比如说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();
         }
    }
}

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

 alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,time_in_milliseconds,alarmManager.INTERVAL_DAY * 14,pendingIntent);
还要寻找解决方案


您可以询问是否还有其他疑问:)

这是一项服务,因为您已经定义了start\u sticky。。在广播接收器的情况下,我相信你有粘性广播。以及启动时的权限。可能的副本必须使用BroadcastReceiver来侦听启动事件,并且在发生时“手动”重新设置警报。请注意,这意味着您还需要以某种持久方式存储与报警相关的数据,如果您没有这样做的话。两个用户还可以查看我的其他评论中的链接,以了解整个过程的详细信息。@user45678
对于HTC设备,前一个加上这个
@user45678:在数据库或某些持久存储中设置闹钟时保存时间和日期值。当手机重新启动时,获取这些值…您必须添加以下代码,以便在手机启动时执行某些任务感谢@JIGARPANDYA,这真是太棒了helpful@user45678:欢迎。如果答案对你有帮助,请接受。