Skip to content Skip to sidebar Skip to footer

How To Check If Alarm Have Been Set And Running

I have a receiver that start after phone boot like this:

Solution 1:

The only thing you can do is when the user install the app and open it the first time you can set a flag in the SharedPreference that tells you if the Alarm is set or not if its not set set the Alaram .

in your main Activity check in the onCreate method

SharedPreferencesspref= getSharedPreferences("TAG", MODE_PRIVATE);

 Booleanalaram_set= spref.getBoolean("alarm_set_flag", false);  //default is falseif(!alaram_set){
//create your alarm here then set the flag to true

 SharedPreferences.Editoreditor= spref.edit();
        editor.putBoolean("alarm_set_flag", true); // value to store
        editor.commit();
}

Solution 2:

Sadly, you cant query AlarmManager for current Alarms.

Best option to solve your problem would be to cancel your current Alarm and set a new one.

You just have to create an intent that matches the one in the reciever. More info here

so add this to your activity

   AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

     Intent i=new Intent(context, LocationPoller.class);

        i.putExtra(LocationPoller.EXTRA_INTENT,
                  new Intent(context, LocationReceiver.class));
        i.putExtra(LocationPoller.EXTRA_PROVIDER,
                 LocationManager.GPS_PROVIDER);

        PendingIntent pi=PendingIntent.getBroadcast(context, 0, i, 0);
    // Cancel alarmstry {
        AlarmManager.cancel(pi);
    } catch (Exception e) {
        Log.e(TAG, "AlarmManager update was not canceled. " + e.toString());
    }
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                        SystemClock.elapsedRealtime(),
                        PERIOD,
                        pi);

You probably need to change the context of the intent so its the same as the one in the Reciever.

Another Workaround would be to detect if its the first start of the App, and only start it then. But then what happens if user reboots his phone before the first start?

Post a Comment for "How To Check If Alarm Have Been Set And Running"