I've been working on implementing an AppWidget
that can be updated manually. However, for some reason the Widget is not updating except for the first time when it's being displayed, and that update is through the Configuration Activity
which pretty much has the same code as onReceive
which is in the AppWidgetProvider
class.
The summary of what I'm doing is, when the update is needed, I send a broadcast which is registered in the WidgetProvider in the Manifest, upon receiving it in onReceive, I extract the values and update the widget, however, the the widget doesn't get updated. I tried updating it directly in onReceive, tried calling onUpdate manually, and tried updating the widget outside the provider but with no luck. Below is a simplified version of the code, I'm pretty sure I'm missing something simple, any suggestions are welcome, and if you need any further information please let me know, thanks in advance.
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
, but it's not getting updated, I tried all solutions that I found around but no luck, thanks.
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>