简体   繁体   English

如何在没有服务的情况下杀死应用程序后在后台线程中运行代码?

[英]How to run code in background thread after app is killed without Service?

I'm first making a query on a background thread using retrofit.我首先使用改造在后台线程上进行查询。 Then I create a new thread in the onResponse callback.然后我在onResponse回调中创建一个新线程。 I then sleep the thread for 10 seconds and try to execute some code.然后我让线程休眠 10 秒并尝试执行一些代码。 While the thread is sleeping, I exit my app.当线程处于休眠状态时,我退出了我的应用程序。 However the logger is never executed when I exit my app during the thread sleep.但是,当我在线程睡眠期间退出我的应用程序时,永远不会执行记录器。

mainService = RetrofitClient.getInstance().getRetrofit().create(MainService.class);
        mainService.getPosts().enqueue(new Callback<List<Post>>() {
            @Override
            public void onResponse(@NotNull Call<List<Post>> call, @NotNull Response<List<Post>> response) {
                //Running on main thread

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(10000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        //This code is never executed. I don't see it in the logcat.
                        Log.d(TAG, "onCreate: Background thread is still running -- " + Thread.currentThread().getName());

                    }
                }).start();
            }

            @Override
            public void onFailure(@NotNull Call<List<Post>> call, @NotNull Throwable t) {
                Log.e(TAG, "onFailure: Call failed", t);
            }
        });

Why isn't the Log.d(TAG, "onCreate: Background thread is still running -- ".... executed even though it's running on a separate thread?为什么Log.d(TAG, "onCreate: Background thread is still running -- "....即使它在单独的线程上运行,它也没有执行?

This is a simplified example on what I'm trying to do.这是关于我正在尝试做的事情的简化示例。 In my other project, after I make the retrofit call, I want to save the data to SQLite even if the user closes the app.在我的另一个项目中,在我进行改造调用后,即使用户关闭了应用程序,我也想将数据保存到 SQLite。 But in this example, I'm trying to figure out the reason why the logger is not being executed.但是在这个例子中,我试图找出记录器没有被执行的原因。

While the thread is sleeping, I exit my app.当线程处于休眠状态时,我退出了我的应用程序。

On many (most?) devices, that will terminate your process, particularly if you do not have a service running.在许多(大多数?)设备上,这将终止您的进程,尤其是在您没有运行服务的情况下。 Terminating your process terminates all your threads.终止您的进程会终止您的所有线程。

How to run code in background thread after app is killed without Service?如何在没有服务的情况下杀死应用程序后在后台线程中运行代码?

You could enqueue the work using WorkManager , if it is not essential that the work be done right away.如果不需要立即完成工作,您可以使用WorkManager将工作排入WorkManager

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

相关问题 即使应用程序在MI设备中被杀死,如何运行后台服务 - How to run a background service even the app is killed in MI devices Android服务需要在后台运行,直到应用被终止 - Android Service required to run in background until the app is killed Android如何在用户杀死应用程序后继续运行后台服务? - Android How to keep Running the background service when app is killed by user? 如何在 JavaFX 应用程序的后台运行线程? - How to run Thread in background of JavaFX app? 应用程序在后台被杀死后,MapFragment崩溃 - MapFragment Crashes after app is killed on background “杀死”后在后台运行Android应用程序 - Running Android App in the Background after “killed” 即使在服务被系统杀死后,Service 内部的线程也会继续运行 - Thread inside Service keep running even after the service is killed by the system 在Oreo版本中杀死Android应用后,每秒在后台运行API - Run API every second in Background after Android App is killed in Oreo version 即使在使用服务后,应用也被杀死 - App is getting killed even after using Service 应用程序被杀死然后重新启动时,Android服务(在其自己的线程中)死亡 - Android Service (In its own thread) dies when app is killed then restarts
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM