好的。我正在开发一个应用程序,用户可以安排时间静音手机,振动手机等等......
该应用程序看起来很好并且功能正常,直到我们获取代码的取消警报部分。
以下是我尝试取消闹钟的方法....但它无效。
public static void DeleteSpecificAlarm(Context context, int alarmType, int id){
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent updateServiceIntent;
switch(alarmType){
case 0:
updateServiceIntent = new Intent(context, MuteAlarmReciever.class);
break;
case 1:
updateServiceIntent = new Intent(context, VibrateAlarmReciever.class);
break;
case 2:
updateServiceIntent = new Intent(context, CustomAlarmReciever.class);
break;
default:
updateServiceIntent = new Intent(context, MuteAlarmReciever.class);
break;
}
PendingIntent pendingUpdateIntent = PendingIntent.getService(context, id, updateServiceIntent, 0);
// Cancel alarms
try {
alarmManager.cancel(pendingUpdateIntent);
Toast.makeText(context, "Alarm Removed", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Log.e("Manager", "AlarmManager update was not canceled. " + e.toString());
Toast.makeText(context, "AlarmManager update was not canceled!", Toast.LENGTH_LONG).show();
}
}
请求:“包含警报创建代码。”
以下是我创建警报的方法:
public static void saveNewData(final Context context, final int volumeType, final int volumeLevel, final TimePicker timePicker){
ParseQuery query = new ParseQuery("Schedule");
query.fromLocalDatastore();
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
if(e == null){
final int alarmID = objects.size() + 1;
ParseObject scheduledItem = new ParseObject("Schedule");
scheduledItem.put("volumeType", volumeType);
scheduledItem.put("volumeLevel", volumeLevel);
scheduledItem.put("alarmID", alarmID);
int hour;
int minute;
if(Build.VERSION.SDK_INT >= 23){
hour = timePicker.getHour();
minute = timePicker.getMinute();
} else {
hour = timePicker.getCurrentHour();
minute = timePicker.getCurrentMinute();
}
scheduledItem.put("hour", hour);
scheduledItem.put("minute", minute);
final Calendar calendar = Calendar.getInstance(); //Full Current Time
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
Log.i("SchedRinger", "User saved new time. User's selected time was: " + calendar.getTime().toString() + "\n" +
"Reciever ID: " + alarmID + "\n" +
"Alarm Type: " + volumeType + "\n" +
"Volume Level: " + volumeLevel + " (Irrelevent if Alarm Type != 2)");
if(hour > 12){
if(minute < 10)
scheduledItem.put("time", hour - 12 + ":0" + minute + " PM");
else
scheduledItem.put("time", hour - 12 + ":" + minute + " PM");
} else {
if(minute < 10)
scheduledItem.put("time", hour + ":0" + minute + " PM");
else
scheduledItem.put("time", hour + ":" + minute + " PM");
}
scheduledItem.pinInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if(e == null){
Toast.makeText(context, "Saved.", Toast.LENGTH_LONG).show();
ScheduleSpecificAlarm(context, calendar, volumeType, volumeLevel, alarmID);
} else {
Toast.makeText(context, "Failed to save. " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
});
} else {
//TODO: Error
}
}
});
}
private static void ScheduleSpecificAlarm(Context context, Calendar cal, int alarmType, int volumeLevel, int id){
Intent intentAlarm;
switch (alarmType){
case 0:
intentAlarm = new Intent(context, MuteAlarmReciever.class);
break;
case 1:
intentAlarm = new Intent(context, VibrateAlarmReciever.class);
break;
case 2:
intentAlarm = new Intent(context, CustomAlarmReciever.class);
intentAlarm.putExtra("volumeLevel", volumeLevel);
break;
default:
intentAlarm = new Intent(context, MuteAlarmReciever.class);
Toast.makeText(context, "Error. Variable passed in wrong on Alarm Creation. Defaulted to 'mute' alarm", Toast.LENGTH_LONG).show();
break;
}
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, id, intentAlarm, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pendingIntent);
}
我正在使用Parse.com进行保存。如果您不了解API,只需知道您可以在ParseObjects中存储和保存数据。这就像Prefs系统android hasm,但我不必担心通过意图发送它。
Catch声明被不抛出。为什么不工作?有什么想法吗?
答案 0 :(得分:0)
而不是PendingIntent.getService(context, id, updateServiceIntent, 0);
中的0尝试使用PendingIntent.FLAG_UPDATE_CURRENT
答案 1 :(得分:0)
正如文档(https://developer.android.com/reference/android/app/AlarmManager.html#cancel(android.app.PendingIntent))所述,AlarmManager
会致电intent.filterEquals(Intent)
以了解是否与PendingIntent匹配。
我建议你在intent.filterEquals(Intent)
的开头设置一个断点,看看它返回false的位置。
答案 2 :(得分:0)
我明白了。它实际上相当简单,一行代码!在DeleteSpecificAlarm()方法中,我使用getService而不是getBroadcast。改变它修复了整个事情!谢谢你所有的帮助!