【发布时间】:2016-05-25 13:57:25
【问题描述】:
我的一个班级中有一个 AsyncTask。我正在执行 AsyncTask,例如:
mAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
当我调试我的应用程序时,正确调用了 mAsyncTask 的 onPreExecute() 函数,但未调用 doInBackground 函数。 什么可能导致问题?
PlanRouteAsync 代码:-
private class PlanRouteAsync extends AsyncTask<Void, Integer, Void>{
@Override
protected void onPreExecute(){
super.onPreExecute();
mIsRunning = true;
}
@Override
protected Void doInBackground(Void... params){
...
return null;
}
@Override
protected void onProgressUpdate(Integer... values){
super.onProgressUpdate(values);
...
}
@Override
protected void onCancelled(){
super.onCancelled();
...
}
@Override
protected void onCancelled(Void aVoid){
super.onCancelled(aVoid);
....
}
@Override
protected void onPostExecute(Void aVoid){
super.onPostExecute(aVoid);
....
}
}
这是一个公司应用程序,有几个 AsyncTask 在后台运行,但我读过,executeOnExecutor 函数应该并行执行 asycnTask。
我也阅读了这些帖子,但它们没有帮助:
Android SDK AsyncTask doInBackground not running (subclass)
编辑:
我的 build.gradle 文件:
android {
compileSdkVersion 23
buildToolsVersion '23.0.3'
defaultConfig {
minSdkVersion 18
targetSdkVersion 23
}
}
dependencies {
compile project(':mPChartLib')
compile project(':sVGSupport')
compile 'com.google.android.gms:play-services:9.0.0'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'io.github.luizgrp.sectionedrecyclerviewadapter:sectionedrecyclerviewadapter:1.0.4'
}
【问题讨论】:
-
你的
targetSdkVersion是什么? -
不是您问题的直接解决方案,但我建议一般远离 AsyncTask,因为它缺乏与 Activity 生命周期的集成,而且它在 sdk 版本之间的行为不同。还有其他几个更好的解决方案,包括我最喜欢的 RxJava
-
谢谢,我会看看,但我真的不想重写完整的代码:/。您如何看待线程池?
-
您的代码 sn-p 不包含任何信息。你的
onPreExecute()和doInBackground()里面有什么? -
我已经添加了我的 onPreExecute() 函数,doInBackground() 无关紧要,因为它根本没有运行。
标签: android multithreading asynchronous android-asynctask