如何动态地更新窗口小部件(而不是等待30分钟的的onupdate被称为)? [英] How to update a Widget dynamically (Not waiting 30 min for onUpdate to be called)?

查看:144
本文介绍了如何动态地更新窗口小部件(而不是等待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 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在我的窗口小部件,任何想法,为什么?


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

这篇关于如何动态地更新窗口小部件(而不是等待30分钟的的onupdate被称为)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆