3

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

<receiver android:name=".OnBootReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>

</receiver>

in the receiver I run set an alarm like this:

AlarmManager  mgr=(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);
          mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                        SystemClock.elapsedRealtime(),
                        PERIOD,
                        pi);

It works fine, except, of course, when user install app, the alarm will not be set until user reboot phone,.

to go around this, I need to check from my Activity if AlarmManager is set, if not I need to set from Activity.

Hence, how do I check if Alarm manager is already set.

user836026
  • 10,608
  • 15
  • 73
  • 129
  • You may look also at : http://stackoverflow.com/questions/4556670/how-to-check-if-alarmmamager-already-has-an-alarm-set – Basher51 Dec 17 '13 at 16:54

2 Answers2

4

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

SharedPreferences spref = getSharedPreferences("TAG", MODE_PRIVATE);

 Boolean   alaram_set = spref.getBoolean("alarm_set_flag", false);  //default is false

if(!alaram_set){
//create your alarm here then set the flag to true

 SharedPreferences.Editor editor = spref.edit();
        editor.putBoolean("alarm_set_flag", true); // value to store
        editor.commit();
}
confucius
  • 13,127
  • 10
  • 47
  • 66
  • would this work if I run AlarmManager in the different VM, than my activity using option " android:process=":remote" – user836026 Aug 31 '12 at 11:34
  • 1
    if you want to access the shared preference from different processes instead of MODE_PRIVATE use MODE_MULTI_PROCESS – confucius Aug 31 '12 at 11:38
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 alarms
    try {
        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?

Community
  • 1
  • 1
Yalla T.
  • 3,707
  • 3
  • 23
  • 36