How to update widget every minute

can someone tell me the best way to update the widget every minute.

Now I use the stream inside the AppWidget, but sometimes I get a FAILED BINDER TRANSACTION error !!! After this error, I always got a lot of errors, so all the time I can’t change the view in my widget.

thanks

+4
source share
2 answers

Instead of using a stream in an AppWidget, you better serve using the AlarmManager to schedule a recurring intention to update the AppWidget, which your code will handle accordingly.

The advantages of this approach are the ability to adjust the update speed, as well as handle the case of a sleeping device (rather than waking up to run your code or even be blocked from sleep, because your thread is busy).

There are numerous examples on the Internet that should explain all the possibilities of using AlarmManager to enhance your AppWidget update goals.

+5
source

The system sends a broadcast event at the exact start of each minute based on the system clock. Create a service with your widgets and do something like this:

BroadcastReceiver _broadcastReceiver; private final SimpleDateFormat _sdfWatchTime = new SimpleDateFormat("HH:mm"); private TextView _tvTime; @Override public void onStart() { super.onStart(); _broadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context ctx, Intent intent) { if (intent.getAction().compareTo(Intent.ACTION_TIME_TICK) == 0) _tvTime.setText(_sdfWatchTime.format(new Date())); } }; registerReceiver(_broadcastReceiver, new IntentFilter(Intent.ACTION_TIME_TICK)); } @Override public void onStop() { super.onStop(); if (_broadcastReceiver != null) unregisterReceiver(_broadcastReceiver); } 

Do not forget, however, to initialize your TextView in advance (before the current system time), since most likely you will pull out your interface in the middle of a minute, and the TextView will not be updated until the next minute.

+2
source

All Articles