当用户更改“设置”屏幕上的复选框时,我尝试取消闹钟。
我正在做的是:在我的AlarmReceiver
班级发送一个带有意图额外布尔变量的广播,以强制警报取消,如:
Intent alarmIntent = new Intent("PACKAGE_ALARM");
alarmIntent.putExtra("cancel", true);
getActivity().sendBroadcast(alarmIntent);
并在onReceive()
方法内:
if (intent.hasExtra("cancel")) {
cancelAlarm(context);
}
其中cancelAlarm
是:
private void cancelAlarm(Context context) {
Intent intent = new Intent(context, ReminderService.class);
PendingIntent pendingIntent = PendingIntent
.getService(context, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
}
我已经设置的闹钟是:
Intent intent = new Intent(context, AlarmrService.class);
PendingIntent pIntent = PendingIntent
.getService(context, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
manager.setExact(AlarmManager.RTC, startTime.getTimeInMillis(), pIntent);
我认为我所做的是正确的,所以我不知道为什么当我调用cancel方法并从我的命令行使用adb shell dumpsys alarm
时,我仍然可以阅读:
+2ms running, 0 wakeups, 1 alarms: u0a74:PACKAGEr
*alarm*:PACKAGE/.reminders.AlarmService
u0a74:PACKAGE +2ms running, 0 wakeups:
+2ms 0 wakes 1 alarms: *alarm*:PACKAGE/.reminders.AlarmService
编辑:完整代码
public class AlarmReceiver extends BroadcastReceiver {
private static final String TAG = AlarmReceiver.class.getSimpleName();
public static final int REQUEST_CODE = 999;
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "onReceive MESA STHN onRECEIVE");
if (intent.hasExtra("cancel")) {
Log.i(TAG, "onReceive CANCEL");
cancelAlarm(context);
}
if (intent.hasExtra("schedule")) {
Log.i(TAG, "onReceive SCHEDULE");
scheduleAlarm(context);
}
//Schedule alarm on BOOT_COMPLETED
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
scheduleAlarm(context);
}
}
/* Schedule the alarm based on user preferences */
public static void scheduleAlarm(Context context) {
AlarmManager manager = AlarmManagerProvider.getAlarmManager(context);
String keyReminder = context.getString(R.string.pref_key_reminder);
String keyAlarm = context.getString(R.string.pref_key_alarm);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
boolean enabled = preferences.getBoolean(keyReminder, false);
//Intent to trigger
Intent intent = new Intent(context, ReminderService.class);
PendingIntent operation = PendingIntent
.getService(context, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Log.i("ALARM", "AlarmReceiver 111111");
if (enabled) {
Log.i("ALARM", "AlarmReceiver 2222222");
//Gather the time preference
Calendar startTime = Calendar.getInstance();
try {
String alarmPref = preferences.getString(keyAlarm, "12:00");
SimpleDateFormat format = new SimpleDateFormat("HH:mm", Locale.getDefault());
startTime.setTime(format.parse(alarmPref));
} catch (ParseException e) {
Log.w(TAG, "Unable to determine alarm start time", e);
return;
}
//Start at the preferred time
//If that time has passed today, set for tomorrow
if (Calendar.getInstance().after(startTime)) {
startTime.add(Calendar.DATE, 1);
}
manager.setExact(AlarmManager.RTC, startTime.getTimeInMillis(), operation);
} else {
Log.i(TAG, "scheduleAlarm MESA STO CANCEL");
manager.cancel(operation);
}
}
private void cancelAlarm(Context context) {
Intent intent = new Intent(context, ReminderService.class);
PendingIntent pendingIntent = PendingIntent
.getService(context, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
// either value we have to cancel the previous alarm
alarmManager.cancel(pendingIntent);
}
}
编辑2:来自SettingsFragment的代码,我在那里收听用户首选项的更改:
listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
String keyReminder = getActivity().getString(R.string.pref_key_reminder);
String keyAlarm = getActivity().getString(R.string.pref_key_alarm);
boolean enabled = sharedPreferences.getBoolean(keyReminder, false);
String alarmTime = sharedPreferences.getString(keyAlarm, "");
Intent alarmIntent = new Intent("PACKAGE_ALARM");
if (key.equals(getResources().getString(R.string.pref_key_reminder))) {
if (enabled) {
// schedule
Log.i("SETTINGS", "SCHEDULE ");
alarmIntent.putExtra("schedule", true);
getActivity().sendBroadcast(alarmIntent);
} else {
// cancel
Log.i("SETTINGS", "CANCEL");
alarmIntent.putExtra("cancel", true);
getActivity().sendBroadcast(alarmIntent);
}
} else if (key.equals(getResources().getString(R.string.pref_key_alarm))) {
if (enabled) {
// update
} else {
// nothing
}
}
}
};
和接收者在Manifest中的条目:
<receiver
android:name=".reminders.AlarmReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="PACKAGE_ALARM" />
</intent-filter>
</receiver>