2013-11-09 95 views
1

我有一个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)第二命中按钮和日志说,报警是尝试禁用,但它不是禁用:-(

任何线索?

+0

您是否尝试过使用媒体播放器播放闹钟? – CENT1PEDE

+0

此报警旨在运行一项控制另一项服务的服务:-),因此它不是媒体相关的报警:-) – Vori

+0

您能显示完整的代码吗? – CENT1PEDE

回答

0

你可以检查报警激活使用:

PendingIntent.getService(aContext, ID_FOR_STOP_ALARM,aiStopMyApp, 
         PendingIntent.FLAG_NO_CREATE) != null 

你对null比较是不对的。 PendingIntent.FLAG_NO_CREATE的文档不正确。如果没有匹配PendingIntent,则返回null,否则返回匹配PendingIntent

+0

谢谢。我会尽快检查:-) – Vori

0

我无法得到它,因为如果警报由使用运行检查工作: (PendingIntent.getService(aContext,ID_FOR_STOP_ALARM,aiStopMyApp,PendingIntent.FLAG_NO_CREATE)== NULL)

我只是单纯地改变了它于:

1)乳宁报警的时候,我存储alarmRunning在偏好设置=真知道报警激活

2)解除警报我设置alarmRunning =虚假的偏好设置,知道什么时候该报警禁用

在启动我知道从Prefs的最后一个状态,我可以在必要时重新创建警报

此解决方法有缺陷但可以工作,它只是假定当设置警报时,不管是什么[直到开机],但它shluld如此:-)

看到你在接下来的Qs的。 五