如何动态地更新窗口小部件(而不是等待30分钟的的onupdate被称为)?被称为、部件、而不是、窗口

由网友(迴卟箌旳過去)分享简介:我目前了解的Andr​​oid部件。I am currently learning about widgets in Android.我想创建一个WiFi小工具,将显示SSID时,RSSI(信号)的水平。I want to create a WIFI widget that will display the SS...

我目前了解的Andr​​oid部件。

I am currently learning about widgets in Android.

我想创建一个WiFi小工具,将显示SSID时,RSSI(信号)的水平。

I want to create a WIFI widget that will display the SSID, the RSSI (Signal) level.

不过,我也希望能够给它的数据来自我运行一个服务发送计算音质通过WiFi。

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.

下面是我的一些阅读和快速教程后,有:

Here is what I have after some reading and a quick tutorial:

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);
}

}

以上似乎工作正常,它更新的插件每10秒的SSID。

The above seems to work ok, it updates the SSID on the widget every 10 seconds.

但什么是最efficent方式来获得我的服务,将已经运行在我的窗口小部件定期更新信息?

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?

另外有没有更好的方法来使用Timer和TimerTask更新的窗口小部件,而不是? (避免轮询)

Also is there a better approach to updating the the widget rather than using a timer and timertask? (Avoid polling)

更新

根据Karan的建议,我已经添加了以下code在我的服务:

As per Karan's suggestion I have added the following code in my Service:

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 );

这总是会自动运行,每次的RSSI电平的变化,但它仍然没有更新的TextView在我的窗口小部件,任何想法,为什么?

80072f78无法更新怎么办

This gets run everytime the RSSI level changes but it still never updates the TextView on my widget, any ideas why?

修改

得到它的工作,感谢卡兰

Got it working, thanks Karan

推荐答案

您可以使用下面的code更新窗口小部件的数据:

You can use the following code to update the data of widget :

ComponentName thisWidget = new ComponentName( getContext(), <ProviderClass> );
AppWidgetManager.getInstance( getContext() ).updateAppWidget( thisWidget, rempoteViews );
阅读全文

相关推荐

最新文章