Android widget update called twice after device boot

拟墨画扇 提交于 2019-12-06 09:40:41

Have you found a way to avoid calling the update twice?

You have no control over how many times you are updated. That is up to the home screen and the app widget framework.

import java.util.ArrayList;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;

public class CopyOfWidgetProvider extends AppWidgetProvider {

    private static ArrayList<Integer> widgets = new ArrayList<Integer>();

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] widgetIDs) {
        super.onUpdate(context, appWidgetManager, widgetIDs);

        for (int widgetID : widgetIDs) {
            if (!widgets.contains(widgetID)) {
                widgets.add(widgetID);
                // this code will run only ONCE after reboot
                // loop is necessary in cases where there were more than one
                // instances of widget before reboot
            }
        }
    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!