java - 在 screen_on 上更新 android 小部件的最佳方法是什么。 Android Oreo 是否支持它(API 26)

标签 java android cordova ionic-framework android-widget

我创建了一个运行良好的小部件,然后根据 Google Play 开发者控制台的要求,我将 targetSDK 从 23 更改为 26。

切换到 Android SDK 26 后,我的应用小部件不再在 screen_on/User_present 事件上更新。我可以看到,由于(后台执行限制),Android 26 中的后台运行任务发生了一系列变化。

以下是我的问题?

问题 1 - 如何在每次 screen_on 事件时更新我的​​应用小部件,以便用户在小部件上看到正确的状态?

Q2- 如何每 1 小时定期更新我的应用小部件?

下面是我目前正在使用的代码。

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);
        }
    }

最佳答案

最后,我找到了解决这个问题的办法。

我们可以在这里简单地找到答案:

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

要解决上述问题,我们可以使用报警管理器。

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

所以在打瞌睡模式结束后,或者在维护窗口期间,它会自动执行代码,编写在警报管理器服务中。

您可以在此处获得帮助,以通过这篇 stackoverflow 帖子在您的小部件中使用警报管理器: https://stackoverflow.com/a/14319020/3497865

关于java - 在 screen_on 上更新 android 小部件的最佳方法是什么。 Android Oreo 是否支持它(API 26),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51189219/

相关文章:

java - 需要 : array or java. lang.Iterable

android - 如何从 Android Resources 类中获取样式?

Android html5 输入类型 ="password"和数字键盘

java - java.lang.RuntimeException:无法启动 Activity ComponentInfo:java.lang.ClassCastException:无法转换为android.widget.Button

angular - 无法在 Ionic 服务 --devapp 上解构 `styles` 的属性 'undefined'

java - Pack200/Network Transfer Format Spec SourceDebugExtension 属性的格式规范

java - 安卓:重启定时器

javascript - 为什么我得到 "Uncaught ReferenceError: require is not defined"?

android - 引用错误 : Camera Is Not Defined

java - HQL:选择语句以及使用 'case when then' 给出意外的 token 错误