Frage

Couple of question on Alarm registration and starting service on trigger.

  1. If an alarm is set at couple of mins ahead of current time and then if phone is made switch off, will the alarm trigger on next phone switch on after the schedule time passed?

  2. How to cancel / update pending intent in service? How to get request code in startCommand() method of service?

  3. Will there be a multiple instances of service created if the alarm is triggered after every 10 seconds?

War es hilfreich?

Lösung

  1. If "switch off" means full power down and not just "once shortly press power button to turn screen off" the answer is "no"

  2. I think you can't get request code at all. As the documentation on getService states, the requestCode field is "currently not used". You should pass all your data with Intent (third arg of getService).

  3. Will not. See http://developer.android.com/reference/android/content/Context.html#startService(android.content.Intent)

Every call to this method will result in a corresponding call to the target service's onStartCommand(Intent, int, int) method

Andere Tipps

  1. Store the time of the alarm in SharedPreferences. Then register a receiver for android.intent.action.BOOT_COMPLETED (remembering to add a permission for android.permission.RECEIVE_BOOT_COMPLETED to your manifest), and then in the receiver, which will execute on startup, you can see if the alarm's in SharedPreferences, and if so you can reset it if it hasn't passed yet, or decide what to do if the time has already passed.

  2. See problem with cancel the alarm manager pending intent

  3. No. The service's onCreate will only be called once. Its onStart and onStartCommand will be called each time.

  1. From my own app that I'm developing, if an alarm is set for a time when the phone is turned off, it has been executed on the next phone on/boot. That is without a receiver for BOOT_COMPLETED being present. I am unsure if this is expected behaviour or not, or whether it is consistent over phone variants.

    I believe if you wish to have your alarm execute the intent at its specified time, you need to use a getBroadcast PendingIntent with a WakeLock as other variants of PendingIntent do not guarantee that phone will remain awake long-enough before it shuts down again. This is information from another post here by CommonsWare, that I will try to find and link to.

  2. I believe you can remove the pendingintent sent to the alarm manager using, for example, a function like:

    public void unregisterEvent(PendingIntent sender) { ((AlarmManager) this.getSystemService(Context.ALARM_SERVICE)).cancel(sender); }

    where the PendingIntent has been created exactly as the original intent you are trying to remove. You can update it by supplying the correct id along with a new PendingIntent when calling AlarmManager again:

    AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);

    This is what I use to create/rebuild the PendingIntent:

    PendingIntent.getService(this, uniqueIndexToIntent, theIntentItself, PendingIntent.FLAG_UPDATE_CURRENT);

    The flag will update the intent if it already exists, or will create a new one otherwise.

  3. I don't think it will. However, I'd recommend having your Service call stopSelf() once it has finished doing its work, so that battery usage is minimised. No need to have it running if it has nothing to do!

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top