AsyncTask的DoInBackground()不会调用Android平板电脑平板、电脑、AsyncTask、DoInBackground

2023-09-05 00:01:12 作者:你是我的暖宝宝

工作在Android的我已经使用的AsyncTask类的应用程序,正常工作时,我测试了我的2.3.5上运行的Andr​​oid设备上,但我面临的问题是,同样是工作不适合我的平板电脑4.0.4

测试时,一定要知道,prexecute()被调用,但doInbackground()不会被调用,但是doInbackground()被调用的设备(2.3.5)。

一个我坚信,对于这个问题的原因是平板电脑的处理器比该设备的速度更快,所以可能会出现一些线程问题,行动组为什么,要解决这个问题,我已经使用了一些标志,并用螺纹。睡眠()在do while循环,这样,当条件为真,它的工作原理,但没有运气,我陷在循环本身。这是我的code:

  MyAsyncTask对象=新MyAsyncTask(MainActivity.this);
runOnUiThread(新的Runnable(){
    公共无效的run(){

        尝试 {
            如果(object.isReady()||!object.isStarting()){
                返回;
            }

            object.execute();

            做 {
                视频下载(1000);
            }而(object.isReady()及!&安培; object.isStarting());

            如果(!object.isReady()){
                返回;
            }
        }赶上(InterruptedException异常E){
            e.printStackTrace();
        }

    }
});
 

AsynctaskClass:

 公共类MyAsyncTask扩展的AsyncTask<虚空,虚空,布尔> {

    私人ProgressDialog对话框;
    私人上下文的背景下;
    私人布尔isStarting = FALSE;
    私人布尔isReady = FALSE;


    公共AsyncUpdatesofJquery(上下文的背景下){
        this.context =背景;
        isStarting = TRUE;
        isReady = FALSE;
    }

    公共布尔isStarting(){
        返回isStarting;
    }

    公共布尔isReady(){
        返回isReady;
    }

    @覆盖
    在preExecute保护无效(){

        isStarting = TRUE;
        isReady = FALSE;
        对话框=新ProgressDialog(上下文);
        dialog.setMessage(下载文件,请稍候......);
        dialog.show();
    }

    @覆盖
    保护布尔doInBackground(空... PARAMS){

        isReady = TRUE;
        isStarting = FALSE;
        downloadFiles(上下文); //我的后台任务

        返回true;
    }

    @覆盖
    保护无效onPostExecute(布尔结果){
        super.onPostExecute(结果);
        context.startActivity(新意图(背景下,NewActivity.class));
        dialog.dismiss();
        isReady = FALSE;
        isStarting = FALSE;

    }
}
 
Android深入解析AsyncTask doInBackground不工作 .pdf 互联网文档类资源 CSDN下载

解决方案

在多线程模型2.3.5和4.0.4之间的变化。 的AsyncTask 现在默认为应用程序中的所有子类使用同一个线程(即只有一个AsyncTask的可以同时运行!)。它解释这里:

  

在首次推出,AsyncTasks是连续在一个后台线程中执行。与DONUT开始,此改变为线程允许多个任务,以在并行操作的一个池。与蜂窝出发,任务是在单个线程中执行,以避免由并行执行常见的应用程序的错误。

     

如果你真正想要并行执行,你可以调用executeOnExecutor(java.util.concurrent.Executor类,对象[])与THREAD_POOL_EXECUTOR。

考虑到这一点,它可能是另一个的AsyncTask 在你的应用程序运行,从而$ ​​P $ pventing这一个从不断开始。这可以解释为什么它工作正常,你的2.3.5的设备上,而不是你的4.0.4平板电脑。

Working on an app in android I have used Asynctask class, Works fine when i tested on my Android device running on 2.3.5, but the problem i am facing is, same is not working for my tablet 4.0.4

While testing, got to know that prexecute() is being called but doInbackground() not being called, however doInbackground() is being called on device(2.3.5).

One of the reason i believe for the problem is that the processor of Tablet is much faster than that of device, so may be some threading issues, dats why, to tackle this, i have used some flags, and used Thread.sleep() in a do while loop so that when condition is true, it works, but no luck, i am stuck in the loop itself. Here is my code:

MyAsyncTask object = new MyAsyncTask (MainActivity.this);
runOnUiThread(new Runnable() {
    public void run() {         

        try {
            if (object.isReady() || !object.isStarting()) {
                return;
            }

            object.execute();

            do {
                Thread.sleep(1000);             
            } while (!object.isReady() && object.isStarting());

            if(!object.isReady()) { 
                return;
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }           
});

AsynctaskClass:

public class MyAsyncTask extends AsyncTask<Void, Void, Boolean>{

    private ProgressDialog dialog;
    private Context context;
    private boolean isStarting = false;
    private boolean isReady = false;


    public AsyncUpdatesofJquery(Context context) {
        this.context = context;
        isStarting = true;
        isReady = false;
    }

    public boolean isStarting() {
        return isStarting;
    }

    public boolean isReady() {
        return isReady;
    }

    @Override
    protected void onPreExecute() {

        isStarting = true;
        isReady = false;
        dialog = new ProgressDialog(context); 
        dialog.setMessage("Downloading Files, Please wait...");
        dialog.show();
    }

    @Override
    protected Boolean doInBackground(Void... params) {

        isReady = true;
        isStarting = false;
        downloadFiles(context); // my background task

        return true;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        context.startActivity(new Intent(context, NewActivity.class));
        dialog.dismiss();
        isReady = false;
        isStarting = false;

    }
}

解决方案

The multi-threading model changed between 2.3.5 and 4.0.4. AsyncTask now defaults to having all subclasses in an application using the same thread (i.e. only one AsyncTask can run at a time!). It's explained here:

When first introduced, AsyncTasks were executed serially on a single background thread. Starting with DONUT, this was changed to a pool of threads allowing multiple tasks to operate in parallel. Starting with HONEYCOMB, tasks are executed on a single thread to avoid common application errors caused by parallel execution.

If you truly want parallel execution, you can invoke executeOnExecutor(java.util.concurrent.Executor, Object[]) with THREAD_POOL_EXECUTOR.

With that in mind, it could be that another AsyncTask is running in your app, thereby preventing this one from ever starting. That would explain why it works fine on your 2.3.5 device, but not your 4.0.4 tablet.