I'm trying to implement the update of the widget on a timer.
As a result, the widget is updated only in two cases:
- When starting the device;
- When you remove and re-add the widget.
The problem is reproduced on both the device and the emulator. I use Android 4.2.2.
Widget code.
Java
private static String getDate() { SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy hh:mm:ss"); return dateFormat.format(new Date()); } public static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,int appWidgetId) { RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.info_widget); views.setTextViewText(R.id.appwidget_textHeader, getDate()); appWidgetManager.updateAppWidget(appWidgetId, views); } public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { super.onUpdate(context,appWidgetManager,appWidgetIds); updateAppWidget(context,appWidgetManager,appWidgetIds[0]); } XML
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:initialKeyguardLayout="@layout/info_widget" android:initialLayout="@layout/info_widget" android:minHeight="40dp" android:minWidth="110dp" android:previewImage="@drawable/example_appwidget_preview" android:resizeMode="horizontal|vertical" android:updatePeriodMillis="5000" > </appwidget-provider> Already tried a lot. Including some solutions from this site, but so far to no avail.
However, it is likely that I am doing something wrong.