假设我有一个班级,我HandlerThread
用Application
它来做一些背景工作。我需要自己停止这个线程还是会被操作系统连同Application
实例一起杀死?我的代码看起来像这样。
public class MyApplication extends Application {
private Handler handler;
@Override
public void onCreate() {
super.onCreate();
HandlerThread handlerThread = new HandlerThread("WorkerThread");
handlerThread.start();
this.handler = new Handler(handlerThread.getLooper());
}
public void runInBackground(Runnable runnable) {
this.handler.post(runnable);
}
}