What is the best way to update android widget on screen_on. Is it supported in Android Oreo (API 26)

别等时光非礼了梦想. 提交于 2020-01-01 03:20:35

问题


I created a widget, which was working quite fine, and then I changed targetSDK from 23 to 26, due to the requirement from Google Play Developer Console.

After switching to Android SDK 26, my app widget is no more updating on the screen_on/User_present event. I can see that there are a bunch of changes in Android 26 for background running task due to (Background Execution Limits).

Following are my questions?

Q1- How can I update my app widget on every screen_on event, so that the user will see the right status on widget?

Q2- How can I update my app widget periodically after every 1 hour?

Following code I'm currently using.

static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                                int appWidgetId) {

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.app_widget);
        String token = getToken();
        getUUID();
        getGatewayActivationStatus();
        getEnvironment();
        if (token != null) {
            remoteViews.setViewVisibility(R.id.layoutBtn, View.VISIBLE);
            AppWidgetIntentReceiver appWI = new AppWidgetIntentReceiver();
            appWI.getMode(context);
        } else {
            remoteViews.setTextViewText(R.id.txt_mode, "Please sign into your App to see status.");
            remoteViews.setViewVisibility(R.id.layoutBtn, View.INVISIBLE);
        }

        remoteViews.setOnClickPendingIntent(R.id.btn_refresh, buildButtonPendingIntent(context, _refresh));
        remoteViews.setOnClickPendingIntent(R.id.txt_mode, buildButtonPendingIntent(context, _openApp));

        appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
    }


    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        for (int appWidgetId : appWidgetIds) {
            updateAppWidget(context, appWidgetManager, appWidgetId);
        }
    }

    @Override
    public void onEnabled(Context context) {
        super.onEnabled(context);
    }

    @Override
    public void onDisabled(Context context) {
        super.onDisabled(context);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);//add this line
        AppWidgetIntentReceiver appWI = new AppWidgetIntentReceiver();

        if (_standby.equals(intent.getAction()) || _home.equals(intent.getAction()) || _away.equals(intent.getAction()) || _refresh.equals(intent.getAction()) || _openApp.equals(intent.getAction())) {
            appWI.handleClickEvent(context, intent, intent.getAction());

        } else if (intent.getAction().equals("android.intent.action.USER_PRESENT") || intent.getAction().equals("android.appwidget.action.APPWIDGET_ENABLED") || intent.getAction().equals("android.intent.action.MY_PACKAGE_REPLACED") || intent.getAction().equals("android.appwidget.action.APPWIDGET_UPDATE")) {

            getToken();
            getUUID();
            getGatewayActivationStatus();
            getEnvironment();
            appWI.handleClickEvent(context, intent, _refresh);

        }
    };

    static PendingIntent buildButtonPendingIntent(Context context, String event) {
        if (!event.equals("OPEN_APP")) {
            Intent intent = new Intent(context, AppWidgetProvider.class);
            intent.setAction(event);
            return PendingIntent.getBroadcast(context, 0, intent, 0);
        } else {
            Intent intent2 = new Intent(context, MainActivity.class);
            return PendingIntent.getActivity(context, 0, intent2, 0);
        }
    }

回答1:


Finally, I've found solution to this problem.

We can simply find answer here:

https://developer.android.com/training/monitoring-device-state/doze-standby

To solve the above problem, we can use Alarm Manager.

Standard AlarmManager alarms (including setExact() and setWindow()) are deferred to the next maintenance window.

So right after the doze mode finishes, or during a maintenance window, it will automatically execute the code, written in alarm manager service.

You can get help here to use Alarm Manager in your widget from this stackoverflow post: https://stackoverflow.com/a/14319020/3497865



来源:https://stackoverflow.com/questions/51189219/what-is-the-best-way-to-update-android-widget-on-screen-on-is-it-supported-in-a

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