我的代码am.set(AlarmManager.ELAPSED_REALTIME,7*AlarmManager.INTERVAL_DAY, pi);
当我重新启动AlarmManager时将取消。如何重新启动broadcastreceiver,我想计算剩余时间。
答案 0 :(得分:0)
如果您想要剩余时间,请保存您设置的时间以在缓存中触发警报
long timeOfTrigger = System.getCurrentcurrentTimeMillis() + 7 * AlarmManager.INTERVAL_DAY;
//use RTC instead of ELAPSED_REALTIME
//RTC calculates absolute time while
//ELAPSED_REALTIME calculates time that has passed since the android system boot.
am.set(AlarmManager.RTC,timeOfTrigger, pi);
Context context = getActivity();
SharedPreferences sharedPref = context.getSharedPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putLong("TimeWhenAlarmStarts", timeOfTrigger );
editor.commit();
这将保存缓存中的触发时间值。然后在任何时候(即使您重新启动了应用程序或手机),您也可以通过
找到剩余时间 SharedPreferences sharedPref = context.getSharedPreferences(Context.MODE_PRIVATE);
long timeOfTrigger = sharedPref.getLong("TimeWhenAlarmStarts" , defaultValue );
long remainingtime = timeOfTrigger - System.getCurrentTimeInMillis();
// the default value can be anything and is used if the SharedPreferences where empty