【问题标题】:Workmanager not working when app is in background当应用程序在后台时,Workmanager 不工作
【发布时间】:2019-07-07 14:31:08
【问题描述】:

我正在尝试定期运行服务,即使应用程序被终止或使用 workManager 在后台运行。

我的 RequestService 类如下:-

public class RequestService extends Worker {

public RequestService(@NonNull Context context, @NonNull WorkerParameters workerParams) {
    super(context, workerParams);
}

@NonNull
@Override
public Result doWork() {

    displayNotification("MY Worker", "Background work Started");
    Log.i("BackJob","Running");
    return Result.SUCCESS;
}

private void displayNotification(String title, String task){

    NotificationManager notificationManager = (NotificationManager)getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel("MyApp","My Notifications",
                                                    NotificationManager.IMPORTANCE_HIGH);

        notificationManager.createNotificationChannel(channel);
    }

    NotificationCompat.Builder notification = new NotificationCompat.Builder(getApplicationContext(), "My Notifications").
                                                    setContentTitle(title).setContentText(task)
                                                            .setSmallIcon(R.mipmap.ic_launcher);

    notificationManager.notify(130, notification.build());

}}

这是主要的活动代码:-

        final PeriodicWorkRequest WorkReq = new PeriodicWorkRequest.Builder(RequestService.class,15,TimeUnit.MINUTES).build();
        WorkManager.getInstance().enqueue(WorkReq);

问题是如果应用程序被杀死或在后台,那么 workmanager 就会停止工作。 我正在使用 android 版本 pie 的三星设备上对此进行测试。

PS :- 如果应用程序已打开,那么我会在 15 分钟后连续看到通知....但是一旦我关闭应用程序.....它就会停止工作......并且没有更多通知

【问题讨论】:

  • 你能看一下WorkManager的codelab源码吗?它实现了一些显示通知的 Worker 类。它包括一个解决相同问题的makeStatusNotificationgithub.com/googlecodelabs/android-workmanager/blob/…
  • 您找到解决方案了吗?我面临着类似的问题。

标签: android android-notifications android-workmanager


【解决方案1】:

您可以为此使用前台服务,前台服务在应用程序处于后台时工作。

将此方法添加到downork方法中

setForegroundAsync(createForegroundInfo(progress));

workermanager 类中重写此方法

 @NonNull
    private ForegroundInfo createForegroundInfo(@NonNull String progress) {

        Context context = getApplicationContext();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel chan = new NotificationChannel("1", "channelName", NotificationManager.IMPORTANCE_NONE);
            chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
            NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            assert manager != null;
            manager.createNotificationChannel(chan);
        }

        Notification notification = new NotificationCompat.Builder(context, "1")
                .setContentTitle("title")
                .setTicker("title")
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setOngoing(true)
                .build();

        return new ForegroundInfo(1,notification);
    }

现在您的应用将在后台运行。

【讨论】:

  • 有人试过这个解决方案吗? - 特别是在 Android 9 和 10 上? -
【解决方案2】:

根据 PeriodicWorkRequest.Builder 官方文档提供here

intervalMillis 必须大于或等于 PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS

此值当前设置为 900000 毫秒,即 15 分钟。

【讨论】:

  • 参考帖子....这就是我正在使用的....15分钟,TimeUnit.MINUTES
  • 好的,你有没有在其他设备上测试过。三星设备存在一些测试问题。
  • 是的,还是没有成功
【解决方案3】:

这是一个工作示例,当前显示有关 SO 版本的任何通知。但似乎问题可能与 NotificationManagerCompat

中的 notify 方法有关
private void makeStatusNotification(String message, Context context) {

    String channelId = context.getString(R.string.worker_sync_notif_channel_id);

    // Make a channel if necessary
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        // Create the NotificationChannel, but only on API 26+
        CharSequence name = context.getString(R.string.worker_sync_notif_channel_name);
        String description = context.getString(R.string.worker_sync_notif_channel_description);
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel channel = new NotificationChannel(channelId, 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, channelId)
            .setSmallIcon(R.drawable.ic_cloud_upload)
            .setContentTitle(context.getString(R.string.worker_sync_notif_title))
            .setContentText(context.getString(R.string.worker_sync_notif_subject))
            .setStyle(new NotificationCompat.BigTextStyle()
                    .bigText(message))
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setVibrate(new long[0])
            .setAutoCancel(true);

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

【讨论】:

  • 抱歉,rply 迟到了.....谢谢您的回答....我可以显示通知,但是当应用程序处于后台时,workmanager 会停止定期请求
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-27
  • 1970-01-01
相关资源
最近更新 更多