Note that there are some explanatory texts on larger screens.

plurals
  1. POFrequent Android Widget Updates
    primarykey
    data
    text
    <p>Greetings,</p> <p>I'm trying to write a clock widget that displays Epoch time and I need it to update every second. Currently I'm doing this using a Service that uses a Handler:</p> <pre><code> public class EpochService extends Service { private static final String TAG = "EpochService"; // the time private static long mTime; // ui components private static RemoteViews mViews; private static ComponentName mComponent; private static AppWidgetManager mManager; // handler private static Handler mHandler = new Handler(); @Override public void onStart(Intent intent, int id){ Log.i(TAG, "start"); // register the receiver IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_SCREEN_OFF); filter.addAction(Intent.ACTION_SCREEN_ON); registerReceiver(mIntentReceiver, filter); // set up ui mViews = new RemoteViews(this.getPackageName(), R.layout.main); mManager = AppWidgetManager.getInstance(this); mComponent = new ComponentName(this, Clock.class); // start mHandler.removeCallbacks(mEpochTimerTask); mHandler.postDelayed(mEpochTimerTask, 1000); } @Override public void onDestroy(){ // unregister receiver to prevent memory leak mHandler.removeCallbacks(mEpochTimerTask); unregisterReceiver(mIntentReceiver); } @Override public IBinder onBind(Intent intent) { return null; } /** * The broadcast receiver - handles screen off events to stop drawing */ private static BroadcastReceiver mIntentReceiver = new BroadcastReceiver(){ @Override public void onReceive(Context context, Intent intent) { if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){ Log.i(TAG, "stop drawing"); mHandler.removeCallbacks(mEpochTimerTask); } else { Log.i(TAG, "start drawing"); mHandler.removeCallbacks(mEpochTimerTask); mHandler.postDelayed(mEpochTimerTask, 1000); } } }; /** * Run the Epoch Timer */ private static Runnable mEpochTimerTask = new Runnable(){ @Override public void run() { Log.i(TAG, "run"); mTime = System.currentTimeMillis() / 1000; mViews.setTextViewText(R.id.time, Long.toString(mTime)); mManager.updateAppWidget(mComponent, mViews); mHandler.postAtTime(mEpochTimerTask, SystemClock.uptimeMillis() + 1000); } }; } </code></pre> <p>The problem I'm having is that over time, my home screen appears to get noticeably laggy. I'm fairly new to Android so I'm not sure if what I'm doing is the correct approach or if there's a better way to achieve this (maybe with AsyncTask?). Is there a way to update a widget frequently without suffering from UI lag? If I need to post more code, please let me know. My widget simply starts and stops this service, and my appwidget-provder has android:updatePeriodMillis="0".</p> <p>Thanks in advance for the help.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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