Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Note: This post has been updated to include the <code>JobScheduler</code> API of the Android Lollipop release. The following is still a viable way, but can be considered deprecated if you're targeting Android Lollipop and beyond. See the second half for the <code>JobScheduler</code> alternative.</p> <p>One way to do recurrent tasks is this:</p> <ul> <li><p>Create a class <code>AlarmReceiver</code></p> <pre><code>public class AlarmReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Intent myService = new Intent(context, YourService.class); context.startService(myService); } } </code></pre> <p>with <code>YourService</code> being your service ;-)</p></li> </ul> <p><strong>If you require a wake lock for your Task, it is advisable to extend from <a href="https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html"><code>WakefulBroadcastReceiver</code></a>. Don't forget to add the <code>WAKE_LOCK</code> permission in your Manifest in this case!</strong></p> <ul> <li>Create a Pending Intent</li> </ul> <p>To start your recurrent polling, execute this code in your activity:</p> <pre><code>Intent myAlarm = new Intent(getApplicationContext(), AlarmReceiver.class); //myAlarm.putExtra("project_id", project_id); //Put Extra if needed PendingIntent recurringAlarm = PendingIntent.getBroadcast(getApplicationContext(), 0, myAlarm, PendingIntent.FLAG_CANCEL_CURRENT); AlarmManager alarms = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); Calendar updateTime = Calendar.getInstance(); //updateTime.setWhatever(0); //set time to start first occurence of alarm alarms.setInexactRepeating(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, recurringAlarm); //you can modify the interval of course </code></pre> <p>This code sets up an <code>alarm</code> and a canceable <code>pendingIntent</code>. The <code>alarmManager</code> gets the job to repeat the <code>recurringAlarm</code> every day (third argument), but <em>inexact</em> so the CPU does wake up approximately after the interval but not exactly (It lets the OS choose the optimal time, which reduces battery drain). The first time the alarm (and thus the service) is started will be the time you choose to be <code>updateTime</code>.</p> <ul> <li><p>last but not least: here is how to kill the recurring alarm</p> <pre><code>Intent myAlarm = new Intent(getApplicationContext(), AlarmReceiver.class); //myAlarm.putExtra("project_id",project_id); //put the SAME extras PendingIntent recurringAlarm = PendingIntent.getBroadcast(getApplicationContext(), 0, myAlarm, PendingIntent.FLAG_CANCEL_CURRENT); AlarmManager alarms = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE); alarms.cancel(recurringAlarm); </code></pre></li> </ul> <p>This code creates a copy of your (probably) existing alarm and tells the <code>alarmManager</code> to cancel all alarms of that kind.</p> <ul> <li>of course there is also something to do in the <code>Manifest</code>:</li> </ul> <p>include these two lines</p> <pre><code> &lt; receiver android:name=".AlarmReceiver"&gt;&lt;/receiver&gt; &lt; service android:name=".YourService"&gt;&lt;/service&gt; </code></pre> <p>inside the <code>&lt; application&gt;</code>-tag. Without it, the system does not accept the start of recurrent alarm of a service.</p> <hr> <p>Starting with the <strong>Android Lollipop</strong> release, there's a new way of solving this task elegantly. This also makes it easier to only perform an action if certain criteria such as network state are met.</p> <pre><code>// wrap your stuff in a componentName ComponentName mServiceComponent = new ComponentName(context, MyJobService.class); // set up conditions for the job JobInfo task = JobInfo.Builder(mJobId, mServiceComponent) .setPeriodic(mIntervalMillis) .setRequiresCharging(true) // default is "false" .setRequiredNetworkCapabilities(JobInfo.NetworkType.UNMETERED) // Parameter may be "ANY", "NONE" (=default) or "UNMETERED" .build(); // inform the system of the job JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE); jobScheduler.schedule(task); </code></pre> <p>You may also provide a deadline with <code>setOverrideDeadline(maxExecutionDelayMillis)</code>.</p> <p>To get rid of such a task, just call <code>jobScheduler.cancel(mJobId);</code> or <code>jobScheduler.cancelAll();</code>.</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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