Asynctask DoInBackground () 未在 Android 平板电脑中调用 [英] Asynctask DoInBackground () not called in Android Tablet

查看:18
本文介绍了Asynctask DoInBackground () 未在 Android 平板电脑中调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 android 中开发应用程序我使用了 Asynctask 类,当我在运行 2.3.5 的 Android 设备上测试时工作正常,但我面临的问题是,同样不适用于我的平板电脑 4.0.4

在测试时,知道正在调用 prexecute() 但没有调用 doInbackground(),但是在设备 (2.3.5) 上调用 doInbackground().

我认为这个问题的原因之一是平板电脑的处理器比设备的处理器快得多,所以可能是一些线程问题,为什么,为了解决这个问题,我使用了一些标志,并使用了线程.sleep() 在 do while 循环中,以便当条件为真时,它可以工作,但没有运气,我被困在循环本身中.这是我的代码:

MyAsyncTask object = new MyAsyncTask (MainActivity.this);runOnUiThread(new Runnable() {公共无效运行(){尝试 {if (object.isReady() || !object.isStarting()) {返回;}object.execute();做 {线程睡眠(1000);} while (!object.isReady() && object.isStarting());if(!object.isReady()) {返回;}} catch (InterruptedException e) {e.printStackTrace();}}});

异步任务类:

public class MyAsyncTask extends AsyncTask{私人 ProgressDialog 对话框;私有上下文上下文;私有布尔值 isStarting = false;私有布尔值 isReady = false;public AsyncUpdatesofJquery(上下文上下文){this.context = 上下文;isStarting = true;isReady = 假;}公共布尔 isStarting() {return isStarting;}公共布尔 isReady() {返回 isReady;}@覆盖受保护的无效 onPreExecute() {isStarting = true;isReady = 假;对话框 = 新的 ProgressDialog(context);dialog.setMessage("正在下载文件,请稍候...");对话框显示();}@覆盖受保护的布尔 doInBackground(Void... params) {isReady = 真;isStarting = false;下载文件(上下文);//我的后台任务返回真;}@覆盖protected void onPostExecute(Boolean 结果) {super.onPostExecute(result);context.startActivity(new Intent(context, NewActivity.class));dialog.dismiss();isReady = 假;isStarting = false;}}

解决方案

多线程模型在 2.3.5 和 4.0.4 之间发生了变化.AsyncTask 现在默认让应用程序中的所有子类都使用同一个线程(即一次只能运行一个 AsyncTask!).在此处进行了说明:

<块引用>

首次引入时,AsyncTasks 在单个后台线程上串行执行.从 DONUT 开始,这已更改为允许多个任务并行操作的线程池.从HONEYCOMB开始,任务在单线程上执行,避免并行执行导致的常见应用错误.

如果你真的想要并行执行,你可以使用 THREAD_POOL_EXECUTOR 调用 executeOnExecutor(java.util.concurrent.Executor, Object[]).

考虑到这一点,可能是另一个 AsyncTask 正在您的应用中运行,从而阻止了这个应用的启动.这将解释为什么它在您的 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.

这篇关于Asynctask DoInBackground () 未在 Android 平板电脑中调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆