android中的异步任务方法调用非常慢

时间:2016-03-08 14:57:05

标签: android multithreading asynchronous android-asynctask

我是一个异步任务方法,它调用后台进程。当我调用此summaryCalc方法时,preexecute方法在此方法调用时运行,但doInBackground方法需要超过20秒才能启动。这需要很长时间。有没有其他方法来提高调用doInBackground方法的速度或任何其他最快的方式来执行线程?谢谢。

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_summary_date_select);
    btnSearch = (Button) findViewById(R.id.btnSearch);

    btnSearch.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
                summaryCalc();                
        }
    });
}
 /**
 * method to create asynchronous task to realign summary data
 */
public void summaryCalc() {
    new AsyncTask<Void, Void, String>() {
        ProgressDialog dialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog = new ProgressDialog(SummaryDateSelectActivity.this);
            dialog.setTitle(getResources().getString(R.string.app_name));
            dialog.setMessage(getResources().getString(R.string.please_wait));
            dialog.setCanceledOnTouchOutside(false);
            dialog.show();
        }

        @Override
        protected String doInBackground(Void... params) {
            ExtraSettingsDS settingsDS = new ExtraSettingsDS(getApplicationContext());
            ExtraSettingsDO settingsDO = settingsDS.getExtraSettingsValues();
            WeeklySummaryRecovery summaryRecovery = new WeeklySummaryRecovery(getApplicationContext());
             /*Insert missing account order data*/
            summaryRecovery.insertMissingAccOrderData();

            if (settingsDO.getAccManage() == 0) {
                summaryRecovery.summaryInsertForSeparateAccManage();
            } else {
                summaryRecovery.summaryInsertForJoinAccManage();
            }
            settingsDS.updateWeeklyFinishedDate();
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            dialog.dismiss();
            intent = new Intent(getApplicationContext(), SummaryDetailsShowActivity.class);
            intent.putExtra(KandhaConstants.IE_NEXT_ACTIVITY, accCheck);
            intent.putExtra(KandhaConstants.IE_DAY_OF_LINE, currentDay);
            intent.putExtra(KandhaConstants.IE_START_DATE, date);
            startActivity(intent);
            finish();
        }

    }.execute(null, null, null);
}

1 个答案:

答案 0 :(得分:1)

您可能正在运行许多异步任务。调用.execute()将逐个执行它们。请尝试拨打.executeOnExecutor()

http://developer.android.com/reference/android/os/AsyncTask.html