0

I have a class to turn on the AlarmManager. I want to call my startMonitoring only when the AlarmManager does not start. The below functions are to start and stop the AlarmManager, but they do not have functions to checking the AlarmManager is starting/stopping (statement). How can I make a function check it?

public static class MonitorBR extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "onReceive : MonitorBR");
        if (ServiceUtil.isRunningService(context, ReadService.class) == false) {           
            context.startService(new Intent(context, ReadService.class));                  
        }
    }
}


public void setInterval(long interval) {
    this.interval = interval;
}


public void startMonitoring(Context context) {
    am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    intent = new Intent(context, MonitorBR.class);
    sender = PendingIntent.getBroadcast(context, 0, intent, 0);
    am.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), interval, sender);

public void stopMonitoring(Context context) {
    am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    intent = new Intent(context, MonitorBR.class);
    sender = PendingIntent.getBroadcast(context, 0, intent, 0);
    am.cancel(sender);
    am = null;
    sender = null;
}
Kaushik
  • 6,150
  • 5
  • 39
  • 54
Jame
  • 3,746
  • 6
  • 52
  • 101
  • You can use preferences to save boolean when start/stop alarm. And after that you can check value in your new method. – Mykhailo Oct 13 '16 at 12:42
  • Maybe this would help,check it out: http://stackoverflow.com/a/9575569/7007542 – R0bert Oct 13 '16 at 12:48

0 Answers0