Workmanager在待机和打ze模式下不起作用

时间:2018-08-31 17:38:22

标签: android push-notification android-workmanager android-doze-and-standby

我正在使用安排我的通知的WorkManager,当我的智能手机处于活动状态时它可以工作,但是当我的智能手机处于待机状态时,它就无法工作。为什么?有一些约束让我知道吗?还是什么?

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

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;
        }
    }
}
MovieAdapter.java

中的

方法 scheduleNotify deleteNotify

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

2 个答案:

答案 0 :(得分:0)

SELECT P.PersonID, P.Email, PT.pType FROM Person P INNER JOIN PersonType PT ON P.PersonID = PT.PersonID LEFT JOIN PersonType PT2 ON P.PersonID = PT2.PersonID AND PT2.ptype != 2 LEFT JOIN PersonType PT3 ON P.PersonID = PT3.PersonID AND PT3.pType != 1 WHERE PT2.ptype IS NOT NULL AND PT3.pType IS NOT NULL; 使用WorkManager,它将在维护时段内批处理作业以优化电池使用。

此外,here是您要查找的约束。

答案 1 :(得分:0)

从1.0.0-alpha06版本起
修复了在打ze模式下导致PeriodicWork无法按计划运行的错误。

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