Android AppWidget not updating



我一直在努力实现可以手动更新的 AppWidget 。但是,由于某种原因,小部件除了首次显示时没有更新,并且该更新是通过 Configuration Activity 具有与 onReceive 的代码相同的代码。在 AppWidgetProvider class。

我正在做的事情的摘要是,当需要更新时,我发送了一个广播,该广播在清单中的widgetprovider中注册,一旦在OnReceive中接收到它,我提取值并更新窗口小部件,但是小部件不会更新。我尝试在OnReceive中直接更新它,尝试手动致电Update,并尝试更新提供商外部的小部件,但没有运气。以下是代码的简化版本,我敢肯定我缺少一些简单的东西,欢迎任何建议,如果您需要任何其他信息,请让我知道,谢谢。

Note that the widget is appearing fine, the initial layout is shown, and the onReceive method is receiving the broadcast correctly since the logs are shown in the LogCat ,但是它没有更新,我尝试了我发现的所有解决方案,但没有运气,谢谢。

onReceive Method:

//onReceive
@Override
public void onReceive(Context context, Intent intent)
{
    if (intent.getAction().equals("com.example.android.myproject.REQUEST_WIDGET_UPDATE"))
    {
        //ComponentName, RemoteViews and AppWidgetManager
        ComponentName provider = new ComponentName(context.getApplicationContext(), ReceiverAppWidgetProvider.class);
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
        AppWidgetManager manager = AppWidgetManager.getInstance(context.getApplicationContext());
        //Change the TextView value
        String timeLeft = intent.getStringExtra("timeLeft");
        views.setTextViewText(R.id.txtv_time_remaining, timeLeft);
        //Update the widget
        manager.updateAppWidget(provider, views);
        //Test calling onUpdate
        //onUpdate(context, manager, manager.getAppWidgetIds(provider));
        //Log the values for the objects for debugging
        MyLog.log(timeLeft);
        MyLog.log(intent.getAction());
        MyLog.log(provider.toString());
        MyLog.log(views.toString());
        MyLog.log(manager.toString());
    }
    else
    {
        super.onReceive(context, intent);
    }
}

Broadcast in Manifest:

<!-- App Widget Provider -->
    <receiver 
        android:name="com.example.android.myproject.widgets.ReceiverAppWidgetProvider"
        android:label="@string/widget_title"
        android:icon="@drawable/background_splash_screen">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>   
            <action android:name="com.example.android.myproject.REQUEST_WIDGET_UPDATE"/>
        </intent-filter>
        <meta-data 
            android:name="android.appwidget.provider"
            android:resource="@xml/app_widget_info"/>
    </receiver>

Widget XML configuration:

<appwidget-provider 
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:minHeight="40dp"
     android:minWidth="250dp"
     android:updatePeriodMillis="0"
     android:resizeMode="none"
     android:initialLayout="@layout/widget_layout"
     android:configure="com.example.android.myproject.AppWidgetConfigureActivity">
</appwidget-provider>

找到了问题,只需重新启动设备使其再次工作,代码本身没有任何问题。如果您使用仿真器,也可能会出现问题,因此重新启动也可以解决问题。

如何更新Android AppWidget的UI

Reported issue: https://code.google.com/p/android/issues/detail?id=8889

最新更新