Android AsyncTask在进入doInBackground()之前需要很长时间

Android AsyncTask在进入doInBackground()之前需要很长时间,android,multithreading,android-asynctask,Android,Multithreading,Android Asynctask,我有一个从SharedReferences读取的异步任务。安装后第一次启动应用程序时,AsyncTask需要很长时间才能调用doInBackground()方法 代码如下: public class ReadSharedPrefsTask extends AsyncTask{ private static final String TAG = ReadSharedPrefsTask.class.getSimpleName(); Context context; Stri

我有一个从SharedReferences读取的异步任务。安装后第一次启动应用程序时,AsyncTask需要很长时间才能调用
doInBackground()
方法

代码如下:

public class ReadSharedPrefsTask extends AsyncTask{

    private static final String TAG = ReadSharedPrefsTask.class.getSimpleName();
    Context context;
    String key;
    Object result;
    OnReadFinishedListener listener;
    long startTime;

    /**
     * @param context the context
     * @param key the shared preferences key
     * @param listener a listener which gets informed when the read operation is finished
     */
    public ReadSharedPrefsTask(Context context, String key, OnReadFinishedListener listener){
        Log.d(TAG, "Ctor begin");
        startTime = System.currentTimeMillis();
        this.context = context.getApplicationContext();
        this.key = key;
        this.listener = listener;
        Log.d(TAG, "Ctor end; elapsed time: " + (System.currentTimeMillis() - startTime));

    }

    @Override
    protected Object doInBackground(Object[] params) {

        Log.d(TAG, "doInBackground; elapsed time: " + (System.currentTimeMillis() - startTime));

        SharedPreferences prefs = MASharedPreferences.get(context);

        Log.d(TAG, "doInBackground; elapsed time: " + (System.currentTimeMillis() - startTime));

        result = prefs.getAll().get(key);

        Log.d(TAG, "doInBackground; elapsed time: " + (System.currentTimeMillis() - startTime));

        return result;
    }

    @Override
    protected void onPostExecute(Object o) {

        Log.d(TAG, "onPostExecute; elapsed time: " + (System.currentTimeMillis() - startTime));
        super.onPostExecute(o);
        listener.onReadFinished(o, key);
    }

}
这就是我对任务的称呼:

new ReadSharedPrefsTask(getContext(), PREF_COUPON_IDS, this).execute();
和logcat打印:

D/ReadSharedPrefsTask﹕ Ctor begin
D/ReadSharedPrefsTask﹕ Ctor end; elapsed time: 3
D/ReadSharedPrefsTask﹕ doInBackground; elapsed time: 126478
D/ReadSharedPrefsTask﹕ doInBackground; elapsed time: 126480
D/ReadSharedPrefsTask﹕ doInBackground; elapsed time: 126481
D/ReadSharedPrefsTask﹕ onPostExecute; elapsed time: 126481
正如您所看到的,完成构造函数和调用doInBackground之间的时间超过两分钟

我在Samsung Galaxy S3(4.2.2)和Nexus 4(5.1)上测试了它,这两款设备都是如此。但它不会发生在genymotion emulator上


有人知道这种行为吗?可能是什么原因造成的?

可能您正在实例化一个新任务,然后调用
execute()
。也就是说,如果您正在呼叫:

ReadSharedPrefsTask task = new ReadSharedPrefsTask();
//maybe some other code here?
task.execute();
尝试将呼叫链接到:

new ReadSharedPrefsTask().execute();

在此之前是否执行过任何其他异步任务,已启动的android API 11异步任务默认按顺序执行

要独立运行任务,必须使用AsyncTask.THREAD\u POOL\u执行器。这需要不同的执行人:

   // Go parallel!
   task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

显示创建和运行tasknice的代码提示,我会尝试一下你是对的!到现在为止,我还不能用你的代码重现这个问题。谢谢答复(已接受;)在我的例子中,我没有其他assynctask,但我有两个并行运行的intentservice。。这可能是原因吗?非常感谢。仍然相关!谢谢你,戈米诺。现在我可以在我的大学课上取得好成绩了。