Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To start the service at a specific time, I suggest you create a BroadcastReceiver triggered by an Alarm, which will in turn start your service. </p> <p>First write a BroadcastReceiver like this : </p> <pre><code>public class AlarmReceiver extends BroadcastReceiver { @Override public void onReceive(final Context context, final Intent intent) { context.startService(new Intent(context, MyService.class)); } /** * Schedule the next update * * @param context * the current application context */ private static void scheduleServiceUpdates(final Context context) { // create intent for our alarm receiver (or update it if it exists) final Intent intent = new Intent(context, AlarmReceiver.class); final PendingIntent pending = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); // compute first call time 1 minute from now Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.MINUTE, 10); long trigger = calendar.getTimeInMillis(); // set delay between each call : 24 Hours long delay = 24 * 60 * 60 * 1000; // Set alarm AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); alarmManager.setRepeating(AlarmManager.RTC, trigger, delay, pending); // you can use RTC_WAKEUP instead of RTC to wake up the device } } </code></pre> <p>Then you just need to call the scheduleServiceUpdate method to start the reccuring event. If you only use the RTC type, then if the phone is locked when the alarm should trigger the service, it won't and will wait until the device is unlocked by the user. If you use RTC_Wakeup, the service will start exactly at the time given. </p> <p>Note that there are other methods in the <a href="https://developer.android.com/reference/android/app/AlarmManager.html" rel="nofollow">AlarmManager</a> to trigger events. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload