簡體   English   中英

當應用程序在后台時,Workmanager 不工作

[英]Workmanager not working when app is in background

即使應用程序被終止或在后台使用 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);

問題是如果應用程序被殺死或在后台,那么工作管理器將停止工作。 我正在帶有 android 版本 pie 的三星設備上對此進行測試。

PS :- 如果應用程序是打開的,那么我會在 15 分鍾后連續看到通知......但是一旦我關閉應用程序......它就會停止工作......並且沒有更多的通知

您可以為此使用前台服務,當應用程序在后台時,前台服務工作。

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

現在您的應用程序將在后台運行。

根據此處提供的 PeriodicWorkRequest.Builder 官方文檔

intervalMillis 必須大於或等於 PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS

該值當前設置為 900000 毫秒,即 15 分鍾。

這是一個工作示例,當前顯示有關 SO 版本的任何通知。 但貌似問題可能與來自NotificationManagerCompat通知方法有關

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM