简体   繁体   English

Android WorkManager-应用程序终止后无法正常运行

[英]Android WorkManager - not working well after application kill

I created simple workManager and I want that in background worked for-loop 50 times every 1 second, so it will iterate loop every 1 second and show log. 我创建了一个简单的workManager,我希望它在后台每隔1秒钟进行一次for循环50次,因此它将每1秒钟循环一次并显示日志。 First let me introduce my code. 首先让我介绍一下我的代码。

This is WorkManager class. 这是WorkManager类。

public class WorkerClass extends Worker {

    private static String TAG = "work_tag";

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

    @NonNull
    @Override
    public Result doWork() {
        try {
            loop();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return Result.success();
    }

    private void loop() throws InterruptedException {
        for (int i = 0; i < 50; i++) {
            Thread.sleep(1000);
            Log.d(TAG, "Working: " + i);
        }
    }
}

And here is my MainActivity. 这是我的MainActivity。

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        OneTimeWorkRequest oneTimeWorkRequest = new OneTimeWorkRequest.Builder(WorkerClass.class).build();

        WorkManager.getInstance().enqueue(oneTimeWorkRequest);
    }
}

Please inform me if there's something wrong. 如果有什么问题请通知我。 So when I'm starting app, it shows log every 1 second like this. 因此,当我启动应用程序时,它每1秒显示一次日志,如下所示。

D/work_tag: Working: 1
D/work_tag: Working: 2
D/work_tag: Working: 3
D/work_tag: Working: 4
D/work_tag: Working: 5

All right yes? 好吗 So when I'm killing app (onDestroy), after 30-35 seconds the loop starts again in background. 因此,当我杀死应用程序(onDestroy)时,在30-35秒后,循环又在后台开始。 After that when I'm opening application, the loop starts again, without completing the previous loop. 之后,当我打开应用程序时,循环将再次开始,而不会完成上一个循环。

For example in background loop iterated 25 times of 50 and if I'll open the app, log would be something like this. 例如,在后台循环中,迭代50次的25次,如果我打开该应用程序,则日志将是这样的。

D/work_tag: Working: 25
D/work_tag: Working: 0
D/work_tag: Working: 26
D/work_tag: Working: 1
D/work_tag: Working: 27
D/work_tag: Working: 2
D/work_tag: Working: 28
D/work_tag: Working: 3
D/work_tag: Working: 29
D/work_tag: Working: 4

You see? 你看? After opening app 2 loops start to iterate asynchronously. 打开应用程序2后,循环开始异步迭代。 So first question is how to avoid this to happen (2 loops asynchronously) and second question is why after destroying the application I have to wait 30-35 seconds for working the loop in background? 因此,第一个问题是如何避免这种情况发生(异步发生2个循环),第二个问题是为什么在销毁应用程序后我必须等待30-35秒才能在后台处理循环?

I'm testing this in android 6.0 我正在android 6.0中对此进行测试

In android 4.4 the background task not scheduling at all. 在android 4.4中,后台任务根本不调度。

You are running two different copies of the worker at this point - each time your Activity starts, you are enqueuing a new WorkRequest. 此时,您正在运行该工作器的两个不同副本-每次您的Activity启动时,您都在排队一个新的WorkRequest。 If you want to use only one copy, use a unique work request: https://developer.android.com/reference/androidx/work/WorkManager#enqueueUniqueWork(java.lang.String,%20androidx.work.ExistingWorkPolicy,%20androidx.work.OneTimeWorkRequest) 如果您只想使用一个副本,请使用一个唯一的工作请求: https : //developer.android.com/reference/androidx/work/WorkManager#enqueueUniqueWork(java.lang.String,%20androidx.work.ExistingWorkPolicy,%20androidx .work.OneTimeWorkRequest)

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

相关问题 Android模拟器运行不正常 - The Android Emulator is not working well 如何处理 WorkManager 中的应用终止场景?(解决方法) - How to handle app kill scenario in WorkManager?(workaround) 重新部署应用程序后杀死Quartz的线程 - Kill Quartz's thread after redeploying application 单击 TextView 后,Android 应用程序停止工作 - Android application stops working after clicking on TextView Android WorkManager 和 Worker - Android WorkManager and Worker 如何杀死在后台进程中运行的android应用程序 - How to kill android application running in background process 在RestClient中,RxJava与Android中的Observable不能很好地配合 - In RestClient RxJava is not working well with Observable in Android 当应用程序被杀死时,WorkManager 是否应该已经在工作,或者它是否需要附加一个前台服务? - Android工作室 - Should WorkManager already be working when the app is killed, or does it need to have a foreground service attached? - Android Studio Android WorkManager 返回多个结果 - Android WorkManager returns multiple Results SharedPreferences字符串设置应用程序终止后丢失的数据(Android模拟器) - SharedPreferences String Set data lost after app kill (android emulator)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM