Android 如何动态更新小部件(不等待 30 分钟调用 onUpdate)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2929393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:56:34  来源:igfitidea点击:

How to update a Widget dynamically (Not waiting 30 min for onUpdate to be called)?

androidwidgetdynamic

提问by Donal Rafferty

I am currently learning about widgets in Android.

我目前正在学习 Android 中的小部件。

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

我想创建一个 WIFI 小部件,它将显示 SSID,RSSI(信号)级别。

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.

但我也希望能够从我正在运行的计算 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);
}

}


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

以上似乎工作正常,它每 10 秒更新一次小部件上的 SSID。

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?

但是,从我的服务中获取信息的最有效方法是什么,这些信息将已经在我的小部件上定期更新?

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

还有比使用计时器和计时器任务更好的方法来更新小部件吗?(避免投票)

UPDATE

更新

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


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

每次 RSSI 级别更改时都会运行它,但它仍然永远不会更新我的小部件上的 TextView,为什么?

EDIT

编辑

Got it working, thanks Karan

得到它的工作,谢谢卡兰

回答by Karan

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

您可以使用以下代码更新小部件的数据:

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

回答by Sephy

If you look at the documentation about widgets, you are supposed to be able to set a time between 2 updates :

如果您查看有关小部件文档,您应该能够设置两次更新之间的时间:

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="294dp"
android:minHeight="72dp"
android:updatePeriodMillis="86400000"
android:initialLayout="@layout/example_appwidget"
android:configure="com.example.android.ExampleAppWidgetConfigure" >

With this, you should be able to put any period you want ? (It mentions that the update might not occur directly after your call for it, and henceforce to do it as often as possible but you have to take care of the battery)

有了这个,你应该可以把你想要的任何时期?(它提到更新可能不会在您调用它后立即发生,因此必须尽可能频繁地进行更新,但您必须照顾好电池)