好的,我有一个后台刷新,用户可以在设置中切换。以下是“设置”活动中的代码:
intent = new Intent(context, UpdateScoresService.class);
recurringDownloadDaily = PendingIntent.getService(context,0,intent, 0);
recurringDownloadWeekly = PendingIntent.getService(context, 1, intent, 0);
Preference.OnPreferenceChangeListener refreshListener = new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if(newValue.toString().equals("1")){ /* daily */
background_refresh.setSummary("Scores will be refreshed daily.");
AlarmManager manager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
manager.cancel(recurringDownloadDaily);
manager.cancel(recurringDownloadWeekly);
Log.e("DAILY REFRESH", " ");
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY,10);
calendar.set(Calendar.MINUTE,00);
if(calendar.before(Calendar.getInstance())){
Log.e("AFTER", "10 AM DAILY");
calendar.add(Calendar.DATE, 1);
}
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, recurringDownloadDaily);
}else if(newValue.toString().equals("2")){ /* weekly */
Log.e("WEEKLY REFRESH", " ");
background_refresh.setSummary("Scores will be refreshed weekly.");
AlarmManager manager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
manager.cancel(recurringDownloadDaily);
manager.cancel(recurringDownloadWeekly);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY,10);
calendar.set(Calendar.MINUTE,00);
if(calendar.before(Calendar.getInstance())){
Log.e("AFTER", "10 AM WEEKLY");
calendar.add(Calendar.DATE, 1);
}
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, recurringDownloadWeekly);
}else{ /* manually */
background_refresh.setSummary("Scores will be refreshed manually.");
Log.e("MANUAL REFRESH", " ");
AlarmManager manager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
manager.cancel(recurringDownloadDaily);
manager.cancel(recurringDownloadWeekly);
}
return true;
}
};
background_refresh.setOnPreferenceChangeListener(refreshListener);
如果用户设置每日闹铃,我希望每天早上10点刷新。 对于测试,当我将日期设置为第二天并将时间设置为上午9:59时,警报将在上午10点左右触发。并且可以手动更改手机上的日期和时间,并且每次都会触发。但是,似乎我设置了选项并且只是等到第二天,警报可能会或可能不会触发。我觉得这种情况往往不会发生。
我也有这段代码知道设备何时完成启动以在设备关闭并重新启动后重新安排闹钟:
public void onReceive(Context context, Intent intent) {
Log.e("Boot reciever", "a");
if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
// Toast.makeText(context, "Boot Reciever", Toast.LENGTH_SHORT).show();
Log.e("Boot reciever", "b");
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
intent1 = new Intent(context, UpdateScoresService.class);
recurringDownload = PendingIntent.getService(context,0,intent1, 0);
String refreshRate = sharedPreferences.getString("background_refresh", "1");
if(refreshRate.equals("1")){ /* daily */
daily(context);
}else if(refreshRate.equals("2")){ /* weekly */
weekly(context);
}else { /* manually */
manually(context);
}
}
}
public void daily(Context context){
Log.e("DAILY REFRESH", "BRUH!");
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY,10);
calendar.set(Calendar.MINUTE,00);
if(calendar.before(Calendar.getInstance())){
Log.e("AFTER", "10 AM WEEKLY");
calendar.add(Calendar.DATE, 1);
}
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, recurringDownload);
}
public void weekly(Context context){
Log.e("WEEKLY REFRESH", "BRUH!");
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY,10);
calendar.set(Calendar.MINUTE,00);
if(calendar.before(Calendar.getInstance())){
Log.e("AFTER", "10 AM WEEKLY");
calendar.add(Calendar.DATE, 1);
}
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY * 7, recurringDownload);
}
public void manually(Context context){
Log.e("MANUAL REFRESH", "BRUH!");
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
manager.cancel(recurringDownload);
}
据我所知,如果我在9:55关闭手机并将其重新启动,这似乎也能正常工作,该服务仍在运行。
问题是,它没有在第二天或第几天运行。我不确定为什么,也许服务在后台被杀?我正在考虑将相同的代码添加到MainActivity的onResume来重新安排警报,但这似乎有点过分,我应该能够让它一直运行而不会这样做。
答案 0 :(得分:0)
不确定,但请检查以下几点:
如果您仍然遇到问题,请查看此blog。 (不知道哪里出错了,所以发布我的博客URL以供参考)