Android Widget shows strange image during update

二次信任 提交于 2019-12-02 05:24:01

I had the exact same issue in my widget as you described and searched a lot for a solution and couldn't find any. What I ended up doing is some kind of workaround that seems to work fine in my case. What i did is the following, instead of updating the widgets directly from the onUpdate() method, I started a service that handled the update then killed itself. This solved it on the emulator and on a device that had the widget stuck during update. Here's a sample code:

The AppWidgetProvider:

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    context.startService(new Intent(context, WidgetService.class));
}

The Service:

@Override
public void onStart(Intent intent, int startId) {
    started(intent);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    started(intent);
    return START_STICKY;
}


private void started(Intent intent) {
    //update here the widgets
    Context context = getApplicationContext();
    updateWidgets(context);
    stopSelf();//killing the service
}

private void updateWidgets(Context context) {
    AppWidgetManager appmanager = AppWidgetManager.getInstance(context);
    ComponentName cmpName = new ComponentName(context, widgetClass);
    int[] widgetIds = appmanager.getAppWidgetIds(cmpName);
    RemoteViews rView = new RemoteViews(context.getPackageName(), layoutId);
    for (int wid : widgetIds) {
        //all updates here
        rView.setTextViewText(tvId, desc);
        appmanager.updateAppWidget(wid, rView);
    }
}

Note sure why this workaround solves the issue, but the good thing is that it does Hope this helps

I had this issue on 4.0 & 4.2 when manually sending android.appwidget.action.APPWIDGET_UPDATE broadcast to update the widget.
Fixed by using a custom action, e.g.

<intent-filter>
    <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    <action android:name="android.appwidget.action.APPWIDGET_DISABLED" />
    <action android:name="my.app.action.APPWIDGET_REFRESH" />
</intent-filter>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!