Android-Asyntask doInBackground滞后和用户界面卡住

Android-Asyntask doInBackground滞后和用户界面卡住,android,for-loop,android-asynctask,Android,For Loop,Android Asynctask,我尝试在后台执行繁重的处理,以避免UI滞后。当我在UI线程中执行此操作时,没有响应,但当我在asyntask中执行此操作时,不再有响应,但UI仍会延迟并停滞一段时间。这是我的密码 private class GenerateTask extends AsyncTask<String, Integer, Void> { @Override protected final Void doInBackground(String... lists) {

我尝试在后台执行繁重的处理,以避免UI滞后。当我在UI线程中执行此操作时,没有响应,但当我在asyntask中执行此操作时,不再有响应,但UI仍会延迟并停滞一段时间。这是我的密码

private class GenerateTask extends AsyncTask<String, Integer, Void> {
        @Override
        protected final Void doInBackground(String... lists) {
            for (int j = 0; j < 9000; j++) {
                final Keyword newKeyword = new Keyword();
                newKeyword.setId(j);
                newKeyword.setQuestion_answer_id(j);
                newKeyword.setKeyword("Keyword ke " + j + " " + UtilHelper.getLocation(j % 9));
                newKeyword.setUpdated_at(UtilHelper.getDateTime());
                //i think this is the one who causes the lag, but i still need this run on ui thread
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        UtilDB db = new UtilDB(getActivity().getApplicationContext());
                        db.replaceKeyword(newKeyword);
                    }
                });
            }
            progressDialog.dismiss();
            return null;
        }
    }
私有类GenerateTask扩展异步任务{
@凌驾
受保护的最终无效doInBackground(字符串…列表){
对于(int j=0;j<9000;j++){
final关键字newKeyword=新关键字();
newKeyword.setId(j);
newKeyword.setQuestion\u answer\u id(j);
newKeyword.setKeyword(“关键字ke”+j+“”+UtilHelper.getLocation(j%9));
newKeyword.setUpdated_at(UtilHelper.getDateTime());
//我认为这是导致延迟的原因,但我仍然需要在ui线程上运行
getActivity().runOnUiThread(新的Runnable()){
@凌驾
公开募捐{
UtilDB=新的UtilDB(getActivity().getApplicationContext());
db.replaceKeyword(newKeyword);
}
});
}
progressDialog.disclose();
返回null;
}
}

你所做的错事就是滥用权力。您可以阅读链接文档,以便学习如何使用它(以及使用原因和时间)

删除:

getActivity().runOnUiThread(new Runnable() {
    @Override
    public void run() {
        UtilDB db = new UtilDB(getActivity().getApplicationContext());
        db.replaceKeyword(newKeyword);
    }
});
并替换为AsyncTask中的另一个方法:

public void onPostExecute(String newKeyword) {
    super.onPostExecute(newKeyword);
    UtilDB db = new UtilDB(getActivity().getApplicationContext());
    db.replaceKeyword(newKeyword);
}
这需要更改为:

AsyncTask<String, Integer, String>
异步任务

很抱歉,如果可以的话,我会发表评论(降低声誉)。。。如果我答对了,您希望在循环中写入数据库,然后更新UI吗


是的,事实上我以后需要更新一些UI,这就是为什么我需要在UI线程上运行它

是否可以将两者分开?首先在HandlerThread中写入数据库,然后通知UI通过UiThread进行更新


目前,您正在将9000个元素放入UiThread队列中,等待处理。这是一项艰巨的工作

在数据库中更新时尝试使用后台线程。为它使用处理程序线程您假设它正在针对数据库运行某些东西。。。但是如果他觉得需要在UI线程上运行它(指示触摸视图等),那么有可能这个假设是错误的是的,实际上我以后需要更新一些UI,这就是为什么我需要在UI线程上运行你提到的asyntask的正确用法,但是RunNuithRead和onPostExcecute都在UI线程上运行。它将如何避免延迟?但是,如果我这样做,我只替换一行,我想要的是更新数据库中的每一行,这就是为什么我在每个循环中都替换。实际上,导致延迟的是在UI线程上运行的循环,但我需要这样做。请发布“replaceKeyword”代码,然后标记我,谢谢,实际上,每次替换数据库中的一行时,我都需要更新视图。有可能吗?嗯,我不知道这是否解决了你的延迟问题,但也许你可以在处理程序线程中执行db更新,而不仅仅是在UI线程中更新UI。或者每50、100或500个循环更新一次Ui?(我想用户可能没有明显的延迟,但会放松ui线程)