简体   繁体   English

Workmanager 在待机和休眠模式下不工作

[英]Workmanager doesn't work in standby and doze mode

I'm using WorkManager that schedule my notifications, it works when my smartphone is active, but it doesn't work when my smartphone is in standby.我正在使用 WorkManager 安排我的通知,它在我的智能手机处于活动状态时工作,但在我的智能手机处于待机状态时它不工作。 Why?为什么? There is some constraint that let me that?有一些约束让我这样吗? Or whatever?管他呢?

Method notifyPush in CoreActivity.java CoreActivity.java中的notifyPush方法

public static void notifyPush(String message, Context context)
    {

        //codice di un altro programma

        // Make a channel if necessary
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // Create the NotificationChannel, but only on API 26+ because
            // the NotificationChannel class is new and not in the support library
            CharSequence name = Constants.VERBOSE_NOTIFICATION_CHANNEL_NAME;
            String description = Constants.VERBOSE_NOTIFICATION_CHANNEL_DESCRIPTION;
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
            channel.setDescription(description);

            // Add the channel
            NotificationManager notificationManager =
                    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

            if (notificationManager != null) {
                notificationManager.createNotificationChannel(channel);
            }
        }

        // Create the notification
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setContentTitle(Constants.NOTIFICATION_TITLE)
                .setContentText(message)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setDefaults(DEFAULT_ALL);
                //.setVibrate(new long[0]);

        // Show the notification
        NotificationManagerCompat.from(context).notify(Constants.NOTIFICATION_ID, builder.build());
    }

NotifyWorker.java NotifyWorker.java

package com.example.msnma.movienotifier.notify;


import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log;

import com.example.msnma.movienotifier.CoreActivity;

import androidx.work.Data;
import androidx.work.Worker;

import static com.example.msnma.movienotifier.CoreActivity.notifyPush;
import static com.example.msnma.movienotifier.notify.Constants.KEY_MOVIE;

public class NotifyWorker extends Worker {

    //private static final String TAG = BlurWorker.class.getSimpleName();

    @NonNull
    @Override
    public Worker.Result doWork() {

        Context applicationContext = getApplicationContext();

        String message = getInputData().getString(Constants.KEY_MOVIE);

        //setOutputData(new Data.Builder().putString(
                //KEY_MOVIE, message.toString()).build());

        try {



            notifyPush(message , applicationContext);
            return Worker.Result.SUCCESS;
        } catch (Throwable throwable) {

            // Technically WorkManager will return WorkerResult.FAILURE
            // but it's best to be explicit about it.
            // Thus if there were errors, we're return FAILURE
            Log.e("NotifyWorker", "Error notification", throwable);
            return Worker.Result.FAILURE;
        }
    }
}

Methods scheduleNotify and deleteNotify in MovieAdapter.java MovieAdapter.java中的方法scheduleNotifydeleteNotify

private UUID scheduleNotify(Date d, int position)
    {
        long currentTime= System.currentTimeMillis();
        //Calendar c = new Date;
        long specificTimeToTrigger = d.getTime();
                //d.getTimeToMillis();
        long delayToPass = specificTimeToTrigger - currentTime;

        /*OneTimeWorkRequest compressionWork =
                new OneTimeWorkRequest.Builder(NotifyWorker.class)
                        .setInputData(message)
                        .setInitialDelay(delayToPass, TimeUnit.MILLISECONDS)
                        .build();*/

        //inizialmente è molto semplice la notifica
        OneTimeWorkRequest notifyRequest =
                new OneTimeWorkRequest.Builder(NotifyWorker.class)
                        .setInputData(createInputDataForUri(movies.get(position)))
                        .setInitialDelay(delayToPass,TimeUnit.MILLISECONDS)
                        .build();
        mWorkManager.enqueue(notifyRequest);
        UUID notify_ID = notifyRequest.getId();

        //WorkManager.getInstance().enqueue(compressionWork);

        return notify_ID;

    }

    public void deleteNotify(UUID notify_ID)
    {
        WorkManager.getInstance().cancelWorkById(notify_ID);
    }

WorkManager uses JobScheduler and that will batch jobs during maintenance windows to optimize battery usage. WorkManager使用JobScheduler ,它将在维护窗口期间批处理作业以优化电池使用。

Also, here is the constraint you are looking for.另外, 是您正在寻找的约束。

Since Version 1.0.0-alpha06自版本 1.0.0-alpha06
A bug which causes PeriodicWork to not run on schedule when in Doze mode was fixed.修复了在打瞌睡模式下导致 PeriodicWork 无法按计划运行的错误。

https://issuetracker.google.com/issues/111469837 https://issuetracker.google.com/issues/111469837

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM