我做了我应该做的每一步,但它仍然没有用。 情况: 我有一个bootreceiver,它的工作原理。 我重启后可以看到Test toast。 重启后我也会在Toast中看到:测试:+ i。 (i =未决意图) 我看到每个scheduleAlarm()都会执行,但是当我到达应该安排的时间时,警报就不会启动。
public class SampleBootReceiver extends BroadcastReceiver {
public static final String MY_PREFS_NAME = "alarmPrefs";
SharedPreferences.Editor editor;
private PendingIntent pi;
long INTERVAL_WEEK = 604800000L;
private AlarmManager am;
Context mContext;
int servTimeH;
int servTimeM;
int yourTimeH;
int yourTimeM;
int difTimeH;
int difTimeM;
String timeH;
String timeM;
String dateandtime;
long delay = 120000L;
int day;
int i = 0;
public void onReceive(Context context, Intent intent)
{
mContext = context;
editor = context.getSharedPreferences(MY_PREFS_NAME, 0).edit();
//start shared pref get here in if method.
SharedPreferences sharedPref = context.getSharedPreferences(".Alarms", Context.MODE_PRIVATE);
System.out.println("BOOL: " + sharedPref.getBoolean("Monday", false));
Toast.makeText(mContext, "TESTTT", Toast.LENGTH_LONG).show();
//boolean days
monday = sharedPref.getBoolean("Monday", false);
if(monday){
day = 1;
if(pl13) {
i++;
pi = PendingIntent.getActivity(mContext.getApplicationContext(), i, intent, PendingIntent.FLAG_CANCEL_CURRENT);
scheduleAlarm(day, 13, 00);
showText("i: " + i);
}
}
}
public void scheduleAlarm(int dayOfWeek, int timeH, int timeM){
ComponentName receiver = new ComponentName(mContext, SampleBootReceiver.class);
PackageManager pm = mContext.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
System.out.println("Repeat alarm");
Calendar calNow = Calendar.getInstance();
Calendar calSet = (Calendar) calNow.clone();
calSet.set(Calendar.DAY_OF_WEEK, dayOfWeek);
calSet.set(Calendar.HOUR_OF_DAY, (timeH + difTimeH));
calSet.set(Calendar.MINUTE, (timeM));
System.out.println("Cal set to:" + (timeH + difTimeH) + ":" + timeM);
calSet.set(Calendar.SECOND, 0);
if(calSet.compareTo(calNow) <= 0){
//Today Set timeH passed, count to tomorrow
calSet.add(Calendar.DAY_OF_YEAR, 7);
}
am = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, (calSet.getTimeInMillis()-delay), INTERVAL_WEEK, pi);
}
public void showText(String s){
Toast.makeText(mContext, s, Toast.LENGTH_LONG).show();
}
}