我有一个hudge问题,禁用报警形成AlarmManager在Android上。禁用RepeatingAlarms不起作用[Android平台]
我看了几篇文章,在这里 答案,不知道为什么我的取消()的报警器不工作。
我甚至已经将我的代码更改为静态变量...(并且仍然不起作用) 代码存储在Utility.class中,而不是调用该方法的代码。
public class Utilities {
public static final int ID_FOR_STOP_ALARM = 770;
private static PendingIntent piStopMyApp;
private static Intent aiStopMyApp;
private static Context aContext;
public static boolean toggleAlarmToStopMyApp(Context context) {
if(aContext == null){
aContext = context;
}
if (aiStopMyApp == null) {
aiStopMyApp = new Intent(aContext, MyAppServiceStop.class);
}
PendingIntent piStopMyAppCheck = PendingIntent.getService(aContext,
ID_FOR_STOP_ALARM, aiStopMyApp, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm = (AlarmManager) aContext
.getSystemService(Context.ALARM_SERVICE);
if (piStopMyApp == null) {
piStopMyApp = PendingIntent.getService(aContext, ID_FOR_STOP_ALARM,
aiStopMyApp, PendingIntent.FLAG_UPDATE_CURRENT);
}
if (piStopMyAppCheck == null) {
SharedPreferences p = aContext.getSharedPreferences(
Utilities.APP_SHARED_PREFS, Context.MODE_PRIVATE);
int h = p.getInt(Utilities.ALARM_STOP_H, 23);
int m = p.getInt(Utilities.ALARM_STOP_M, 0);
Calendar cur_cal = new GregorianCalendar();
cur_cal.setTimeInMillis(System.currentTimeMillis());
Calendar cal = new GregorianCalendar();
cal.set(Calendar.YEAR, cur_cal.get(Calendar.YEAR));
cal.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));
cal.set(Calendar.DAY_OF_MONTH, cur_cal.get(Calendar.DAY_OF_MONTH));
cal.set(Calendar.HOUR_OF_DAY, h);
cal.set(Calendar.MINUTE, m);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
if (isLoggingEnabled) {
Log.d(TAG, "Enable ALARM for STOP " + cal.toString());
}
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, piStopMyApp);
} else {
alarm.cancel(piStopMyApp);
if (isLoggingEnabled) {
Log.d(TAG, "Disable ALARM for STOP = "
+ (PendingIntent.getService(aContext,
ID_FOR_STOP_ALARM,aiStopMyApp,
PendingIntent.FLAG_NO_CREATE) == null));
}
}
piStopMyAppCheck = null;
return (PendingIntent.getService(aContext, ID_FOR_STOP_ALARM, aiStopMyApp,
PendingIntent.FLAG_NO_CREATE) != null);
}
}
和它根本不起作用,使报警和不能禁用它:-(在其他活动
按钮运行这样的代码:
boolean alarmUpStop = Utilities.toggleAlarmToStopMyApp(getApplicationContext());
1)先打到按钮上述和报警妥善安排,日志显示服务触发
2)第二命中按钮和日志说,报警是尝试禁用,但它不是禁用:-(
任何线索?
您是否尝试过使用媒体播放器播放闹钟? – CENT1PEDE
此报警旨在运行一项控制另一项服务的服务:-),因此它不是媒体相关的报警:-) – Vori
您能显示完整的代码吗? – CENT1PEDE