Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to update a Widget dynamically (Not waiting 30 min for onUpdate to be called)?
    text
    copied!<p>I am currently learning about widgets in Android.</p> <p>I want to create a WIFI widget that will display the SSID, the RSSI (Signal) level.</p> <p>But I also want to be able to send it data from a service I am running that calculates the Quality of Sound over wifi.</p> <p>Here is what I have after some reading and a quick tutorial:</p> <hr> <pre><code>public class WlanWidget extends AppWidgetProvider{ RemoteViews remoteViews; AppWidgetManager appWidgetManager; ComponentName thisWidget; WifiManager wifiManager; public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { Timer timer = new Timer(); timer.scheduleAtFixedRate(new WlanTimer(context, appWidgetManager), 1, 10000); } private class WlanTimer extends TimerTask{ RemoteViews remoteViews; AppWidgetManager appWidgetManager; ComponentName thisWidget; public WlanTimer(Context context, AppWidgetManager appWidgetManager) { this.appWidgetManager = appWidgetManager; remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget); thisWidget = new ComponentName(context, WlanWidget.class); wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE); } @Override public void run() { remoteViews.setTextViewText(R.id.widget_textview, wifiManager.getConnectionInfo().getSSID()); appWidgetManager.updateAppWidget(thisWidget, remoteViews); } } </code></pre> <hr> <p>The above seems to work ok, it updates the SSID on the widget every 10 seconds.</p> <p>However what is the most efficent way to get the information from my service that will be already running to update periodically on my widget?</p> <p>Also is there a better approach to updating the the widget rather than using a timer and timertask? (Avoid polling)</p> <p><strong>UPDATE</strong></p> <p>As per Karan's suggestion I have added the following code in my Service:</p> <hr> <pre><code>RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget); ComponentName thisWidget = new ComponentName( context, WlanWidget.class ); remoteViews.setTextViewText(R.id.widget_QCLevel, " " + qcPercentage); AppWidgetManager.getInstance( context ).updateAppWidget( thisWidget, remoteViews ); </code></pre> <hr> <p>This gets run everytime the RSSI level changes but it still never updates the TextView on my widget, any ideas why?</p> <p><strong>EDIT</strong></p> <p>Got it working, thanks Karan</p>
 

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