Frequently updating widgets (more frequently than what updatePeriodMillis allows)

后端 未结 1 636
死守一世寂寞
死守一世寂寞 2020-12-23 23:20

I want a widget showing a countdown for a user initiated tracking of a bus departure. I want to update the widget every minute or so, from when the user initiates the tracki

相关标签:
1条回答
  • 2020-12-23 23:47

    I would register an alarm to start my service every 1 minute and the service would update the widget UI

    final Intent intent = new Intent(context, UpdateService.class);
    final PendingIntent pending = PendingIntent.getService(context, 0, intent, 0);
    final AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarm.cancel(pending);
    long interval = 1000*60;
    alarm.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),interval, pending);
    

    AlarmManager.ELAPSED_REALTIME will not wakr the device if it's sleeping to battery life should not be affected.

    0 讨论(0)
提交回复
热议问题