Widget update at short intervals using API 19+

Pre API 19, the method of moving to update the widget faster than the minimum updatePeriodMillis time of 30 minutes, was to use AlarmManager and BroadcastReceiver to get the intent after the specified interval used in setting up AlarmManager.

Currently, using the code below, the widget is updated, but with Android 5.1 using .setRepeating () with a repeat interval of less than 60,000 ms, an interval of at least 60,000 ms will be automatically set.

Setting the alarm in onEnabled () widgets:

 AlarmManager am= (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class); PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0); //After after 3 seconds am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+ 3000, 1000 , pi); 

then in AlarmManagerBroadcastReceiver onReceive ():

 PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "TAG"); //Acquire the lock wl.acquire(); /* * ...... * Update Widgets RemoteViews */ wl.release(); 

The docs for setRepeating () say:

Note: According to API 19, all repeated alarms are inaccurate. If your application needs accurate delivery times, it should use one-time accurate alarms, each time reviewing the dates as described above. Deprecated applications whose targetSdkVersion was previously API 19 will still have all of their alarms, including repeated alarms that are considered accurate.

now also says:

Schedule a repeating signal. Note: for synchronization operations (ticks, timeouts, etc.) it is easier and more efficient to use Handler

So, how are you going to update Remoteviews Widgets with Handler? How do you stop it when the device sleeps to save battery?

Are there any other suggested ways to update the widget?

+6
source share
1 answer

From API level 21, JobScheduler is recommended to handle such periodic updates.

Define the job in the JobService :

 public class UpdateJob extends JobService { public static int JOB_ID=9; @Override public boolean onStartJob(JobParameters params) { Toast.makeText(getApplicationContext(),"update",Toast.LENGTH_SHORT).show(); //call handler, create thread, asynctask etc return false; } @Override public boolean onStopJob(JobParameters params) { return false; } } 

Register it in the manifest:

  <service android:name=".UpdateJob" android:permission="android.permission.BIND_JOB_SERVICE" /> 

Schedule a task e. in the Office:

  JobScheduler mJobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE); JobInfo.Builder builder = new JobInfo.Builder( UpdateJob.JOB_ID, new ComponentName( getApplicationContext(), UpdateJob.class ) ); builder.setPeriodic(3000); // in every 3 sec builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY); // only when network is available if( mJobScheduler.schedule( builder.build() ) <= 0 ) { //error, cant be scheduled } //later eg when update is disabled mJobScheduler.cancel(UpdateJob.JOB_ID); 

JobInfo.Builder has many options for setting when your job starts, for example. it may depend on the status of the network and device.

In lower APIs, JobSchedulerCompat or GCM Network Manager can be used as an alternative to this, they work almost the same way as shown above.

+ one more

To work with the BroadcastReceiver, which wakelock acquires, the support library has a β€œhelper” class, WakefulBroadcastReceiver .

+2
source

All Articles