Android appUI更新速度慢

时间:2015-05-25 08:19:36

标签: android database android-asynctask ui-thread

在我的应用程序中单击一个按钮时,首先更新数据库,然后将该数据发送到服务器。在发送数据之前,数据是加密的。 db的更新是在一个异步任务中完成的,加密和发送数据是通过另一个异步任务完成的,如下所示

1)更新db

 private class UpdateThread extends AsyncTask<Void, Void, Void> {
    private MessageThread messageThread;

    private UpdateThread(MessageThread thread) {
        this.messageThread = thread;
    }

    @Override
    protected Void doInBackground(Void... params) {
        action.update(messageThread);

        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);

        PollReply pollReply = new PollReply(messageThread);
        pollReply.execute();
    }
}

2)加密数据并发送到服务器

private class PollReply extends AsyncTask<Void, Void, Void> {
    private MessageThread messageThread;

    private PollReply(MessageThread thread) {
        this.messageThread = thread;
    }

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

        messageThread.setHashcode(HashUtility.encryptString(messageThread.getRandomcode() + messageThread.getUserId()));
        messageThread.setStrippedHashCode(StringUtility.reduceToBytes(messageThread.getHashcode(), 16));
        try {
            messageThread.setAnswerEnc(EncryptionUtility.encrypt(messageThread.getAnswer(), messageThread.getStrippedHashCode()));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (InvalidKeySpecException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        }


        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);

        PollReplyAsyncTask asyncTask = new PollReplyAsyncTask(activity, false, messageThread, messageThread.getUserId());
        asyncTask.execute();
    }
}

并且这些异步任务被调用如下

UpdateThread updateThread = new UpdateThread(message);
updateThread.execute();

PollReply pollReply = new PollReply(message);
pollReply.execute();

当我评论第二个异步任务时,db会立即更新,UI也会立即更新。 但是当我使用第二个异步任务时,db会更新,但ui会在执行第二个异步任务后更新。我认为UI线程在某处被阻止了。解决这个问题的方法是什么?

if (message.getOptionsCount() > 0) {

                    if (message.getOptionsCount() > 1) {
                        pollChoice1.setText(message.getOption1());
                        pollChoice2.setText(message.getOption2());

                        backgroundWidth = 0;
                        if (message.getOptionsCount() == 2) {
                            pollChoice1.setVisibility(View.VISIBLE);
                            pollChoice2.setVisibility(View.VISIBLE);
                            pollChoice3.setVisibility(View.GONE);
                            pollChoice4.setVisibility(View.GONE);
                            pollChoice5.setVisibility(View.GONE);

                            if ((message.getVisibility().equals("public") && message.getAnswered()) || (message.getType() == MessageThread.OUT_MESSAGE)) {
                                pollChoice3Background.setVisibility(View.GONE);
                                pollChoice4Background.setVisibility(View.GONE);
                                pollChoice5Background.setVisibility(View.GONE);

                                if (responseCount > 0) {
                                    pollChoice1Background.setVisibility(View.VISIBLE);
                                    pollChoice2Background.setVisibility(View.VISIBLE);

                                    float widthFloat = (float) (option1Count * 100.0 / responseCount);
                                    backgroundWidth = (int) widthFloat;
                                    int total_width = pollChoice1.getMeasuredWidth();
                                    pollChoice1Background.setText("" + backgroundWidth + "%");
                                    backgroundWidth = (total_width * backgroundWidth / 100);
                                    pollChoice1Background.getLayoutParams().width = backgroundWidth;

                                    widthFloat = (float) (option2Count * 100.0 / responseCount);
                                    backgroundWidth = (int) widthFloat;
                                    total_width = pollChoice1.getMeasuredWidth();
                                    pollChoice2Background.setText("" + backgroundWidth + "%");
                                    backgroundWidth = (total_width * backgroundWidth / 100);
                                    pollChoice2Background.getLayoutParams().width = backgroundWidth;
                                } else {
                                    pollChoice1Background.setVisibility(View.INVISIBLE);
                                    pollChoice2Background.setVisibility(View.INVISIBLE);
                                }
                            } else {
                                pollChoice1Background.setVisibility(View.INVISIBLE);
                                pollChoice2Background.setVisibility(View.INVISIBLE);
                            }

                            line1choice.setVisibility(View.VISIBLE);
                            line2choice.setVisibility(View.VISIBLE);
                            line3choice.setVisibility(View.VISIBLE);

                            choice1Layout.setVisibility(View.VISIBLE);
                            choice2Layout.setVisibility(View.VISIBLE);
                            choice3Layout.setVisibility(View.GONE);
                            choice4Layout.setVisibility(View.GONE);
                            choice5Layout.setVisibility(View.GONE);
                            choice6Layout.setVisibility(View.GONE);

                        } else if (message.getOptionsCount() == 3) {

                            dps = 150;
                            pixels = (int) (dps * scale + 0.5f);
                            feed.getLayoutParams().height = pixels;

                            pollChoice1.setText(message.getOption1());
                            pollChoice2.setText(message.getOption2());
                            pollChoice3.setText(message.getOption3());

                            pollChoice1.setVisibility(View.VISIBLE);
                            pollChoice2.setVisibility(View.VISIBLE);
                            pollChoice3.setVisibility(View.VISIBLE);
                            pollChoice4.setVisibility(View.GONE);
                            pollChoice5.setVisibility(View.GONE);

                            if ((message.getVisibility().equals("public") && message.getAnswered()) || (message.getType() == MessageThread.OUT_MESSAGE)) {
                                pollChoice4Background.setVisibility(View.GONE);
                                pollChoice5Background.setVisibility(View.GONE);

                                if (responseCount > 0) {
                                    pollChoice1Background.setVisibility(View.VISIBLE);
                                    pollChoice2Background.setVisibility(View.VISIBLE);
                                    pollChoice3Background.setVisibility(View.VISIBLE);


                                    float width_float = (float) (option1Count * 100.0 / responseCount);
                                    backgroundWidth = (int) width_float;
                                    int total_width = pollChoice1.getMeasuredWidth();
                                    pollChoice1Background.setText("" + backgroundWidth + "%");
                                    backgroundWidth = (total_width * backgroundWidth / 100);
                                    pollChoice1Background.getLayoutParams().width = backgroundWidth;

                                    width_float = (float) (option2Count * 100.0 / responseCount);
                                    backgroundWidth = (int) width_float;
                                    total_width = pollChoice2.getMeasuredWidth();
                                    pollChoice2Background.setText("" + backgroundWidth + "%");
                                    backgroundWidth = (total_width * backgroundWidth / 100);
                                    pollChoice2Background.getLayoutParams().width = backgroundWidth;

                                    width_float = (float) (option3Count * 100.0 / responseCount);
                                    backgroundWidth = (int) width_float;
                                    total_width = pollChoice3.getMeasuredWidth();
                                    pollChoice3Background.setText("" + backgroundWidth + "%");
                                    backgroundWidth = (total_width * backgroundWidth / 100);
                                    pollChoice3Background.getLayoutParams().width = backgroundWidth;
                                } else {
                                    pollChoice1Background.setVisibility(View.INVISIBLE);
                                    pollChoice2Background.setVisibility(View.INVISIBLE);
                                    pollChoice3Background.setVisibility(View.INVISIBLE);
                                }
                            } else {
                                pollChoice1Background.setVisibility(View.INVISIBLE);
                                pollChoice2Background.setVisibility(View.INVISIBLE);
                                pollChoice3Background.setVisibility(View.INVISIBLE);
                            }

                            line1choice.setVisibility(View.VISIBLE);
                            line2choice.setVisibility(View.VISIBLE);
                            line3choice.setVisibility(View.VISIBLE);
                            line4choice.setVisibility(View.VISIBLE);

                            choice1Layout.setVisibility(View.VISIBLE);
                            choice2Layout.setVisibility(View.VISIBLE);
                            choice3Layout.setVisibility(View.VISIBLE);
                            choice4Layout.setVisibility(View.GONE);
                            choice5Layout.setVisibility(View.GONE);
                            choice6Layout.setVisibility(View.GONE);
                        } else if (message.getOptionsCount() == 4) {

                            dps = 100;
                            pixels = (int) (dps * scale + 0.5f);
                            feed.getLayoutParams().height = pixels;

                            pollChoice1.setText(message.getOption1());
                            pollChoice2.setText(message.getOption2());
                            pollChoice3.setText(message.getOption3());
                            pollChoice4.setText(message.getOption4());

                            pollChoice1.setVisibility(View.VISIBLE);
                            pollChoice2.setVisibility(View.VISIBLE);
                            pollChoice3.setVisibility(View.VISIBLE);
                            pollChoice4.setVisibility(View.VISIBLE);
                            pollChoice5.setVisibility(View.GONE);

                            if ((message.getVisibility().equals("public") && message.getAnswered()) || (message.getType() == MessageThread.OUT_MESSAGE)) {
                                pollChoice5Background.setVisibility(View.GONE);

                                if (responseCount > 0) {
                                    pollChoice1Background.setVisibility(View.VISIBLE);
                                    pollChoice2Background.setVisibility(View.VISIBLE);
                                    pollChoice3Background.setVisibility(View.VISIBLE);
                                    pollChoice4Background.setVisibility(View.VISIBLE);

                                    float width_float = (float) (option1Count * 100.0 / responseCount);
                                    backgroundWidth = (int) width_float;
                                    int total_width = pollChoice1.getMeasuredWidth();
                                    pollChoice1Background.setText("" + backgroundWidth + "%");
                                    backgroundWidth = (total_width * backgroundWidth / 100);
                                    pollChoice1Background.getLayoutParams().width = backgroundWidth;

                                    width_float = (float) (option2Count * 100.0 / responseCount);
                                    backgroundWidth = (int) width_float;
                                    total_width = pollChoice2.getMeasuredWidth();
                                    pollChoice2Background.setText("" + backgroundWidth + "%");
                                    backgroundWidth = (total_width * backgroundWidth / 100);
                                    pollChoice2Background.getLayoutParams().width = backgroundWidth;

                                    width_float = (float) (option3Count * 100.0 / responseCount);
                                    backgroundWidth = (int) width_float;
                                    total_width = pollChoice3.getMeasuredWidth();
                                    pollChoice3Background.setText("" + backgroundWidth + "%");
                                    backgroundWidth = (total_width * backgroundWidth / 100);
                                    pollChoice3Background.getLayoutParams().width = backgroundWidth;

                                    width_float = (float) (option4Count * 100.0 / responseCount);
                                    backgroundWidth = (int) width_float;
                                    total_width = pollChoice4.getMeasuredWidth();
                                    pollChoice4Background.setText("" + backgroundWidth + "%");
                                    backgroundWidth = (total_width * backgroundWidth / 100);
                                    pollChoice4Background.getLayoutParams().width = backgroundWidth;
                                } else {
                                    pollChoice1Background.setVisibility(View.INVISIBLE);
                                    pollChoice2Background.setVisibility(View.INVISIBLE);
                                    pollChoice3Background.setVisibility(View.INVISIBLE);
                                    pollChoice4Background.setVisibility(View.INVISIBLE);
                                }
                            } else {
                                pollChoice1Background.setVisibility(View.INVISIBLE);
                                pollChoice2Background.setVisibility(View.INVISIBLE);
                                pollChoice3Background.setVisibility(View.INVISIBLE);
                                pollChoice4Background.setVisibility(View.INVISIBLE);
                            }

                            line1choice.setVisibility(View.VISIBLE);
                            line2choice.setVisibility(View.VISIBLE);
                            line3choice.setVisibility(View.VISIBLE);
                            line4choice.setVisibility(View.VISIBLE);
                            line5choice.setVisibility(View.VISIBLE);

                            choice1Layout.setVisibility(View.VISIBLE);
                            choice2Layout.setVisibility(View.VISIBLE);
                            choice3Layout.setVisibility(View.VISIBLE);
                            choice4Layout.setVisibility(View.VISIBLE);
                            choice5Layout.setVisibility(View.GONE);
                            choice6Layout.setVisibility(View.GONE);
                        } else if (message.getOptionsCount() == 5) {

                            dps = 100;
                            pixels = (int) (dps * scale + 0.5f);
                            feed.getLayoutParams().height = pixels;

                            pollChoice1.setText(message.getOption1());
                            pollChoice2.setText(message.getOption2());
                            pollChoice3.setText(message.getOption3());
                            pollChoice4.setText(message.getOption4());
                            pollChoice5.setText(message.getOption5());

                            pollChoice1.setVisibility(View.VISIBLE);
                            pollChoice2.setVisibility(View.VISIBLE);
                            pollChoice3.setVisibility(View.VISIBLE);
                            pollChoice4.setVisibility(View.VISIBLE);
                            pollChoice5.setVisibility(View.VISIBLE);

                            if ((message.getVisibility().equals("public") && message.getAnswered()) || (message.getType() == MessageThread.OUT_MESSAGE)) {
                                if (responseCount > 0) {
                                    pollChoice1Background.setVisibility(View.VISIBLE);
                                    pollChoice2Background.setVisibility(View.VISIBLE);
                                    pollChoice3Background.setVisibility(View.VISIBLE);
                                    pollChoice4Background.setVisibility(View.VISIBLE);
                                    pollChoice5Background.setVisibility(View.VISIBLE);

                                    float width_float = (float) (option1Count * 100.0 / responseCount);
                                    backgroundWidth = (int) width_float;
                                    int total_width = pollChoice1.getMeasuredWidth();
                                    pollChoice1Background.setText("" + backgroundWidth + "%");
                                    backgroundWidth = (total_width * backgroundWidth / 100);
                                    pollChoice1Background.getLayoutParams().width = backgroundWidth;

                                    width_float = (float) (option2Count * 100.0 / responseCount);
                                    backgroundWidth = (int) width_float;
                                    total_width = pollChoice2.getMeasuredWidth();
                                    pollChoice2Background.setText("" + backgroundWidth + "%");
                                    backgroundWidth = (total_width * backgroundWidth / 100);
                                    pollChoice2Background.getLayoutParams().width = backgroundWidth;

                                    width_float = (float) (option3Count * 100.0 / responseCount);
                                    backgroundWidth = (int) width_float;
                                    total_width = pollChoice3.getMeasuredWidth();
                                    pollChoice3Background.setText("" + backgroundWidth + "%");
                                    backgroundWidth = (total_width * backgroundWidth / 100);
                                    pollChoice3Background.getLayoutParams().width = backgroundWidth;

                                    width_float = (float) (option4Count * 100.0 / responseCount);
                                    backgroundWidth = (int) width_float;
                                    total_width = pollChoice4.getMeasuredWidth();
                                    pollChoice4Background.setText("" + backgroundWidth + "%");
                                    backgroundWidth = (total_width * backgroundWidth / 100);
                                    pollChoice4Background.getLayoutParams().width = backgroundWidth;

                                    width_float = (float) (option5Count * 100.0 / responseCount);
                                    backgroundWidth = (int) width_float;
                                    total_width = pollChoice5.getMeasuredWidth();
                                    pollChoice5Background.setText("" + backgroundWidth + "%");
                                    backgroundWidth = (total_width * backgroundWidth / 100);
                                    pollChoice5Background.getLayoutParams().width = backgroundWidth;

                                } else {
                                    pollChoice1Background.setVisibility(View.INVISIBLE);
                                    pollChoice2Background.setVisibility(View.INVISIBLE);
                                    pollChoice3Background.setVisibility(View.INVISIBLE);
                                    pollChoice4Background.setVisibility(View.INVISIBLE);
                                    pollChoice5Background.setVisibility(View.INVISIBLE);
                                }
                            } else {
                                pollChoice1Background.setVisibility(View.INVISIBLE);
                                pollChoice2Background.setVisibility(View.INVISIBLE);
                                pollChoice3Background.setVisibility(View.INVISIBLE);
                                pollChoice4Background.setVisibility(View.INVISIBLE);
                                pollChoice5Background.setVisibility(View.INVISIBLE);
                            }

                            line1choice.setVisibility(View.VISIBLE);
                            line2choice.setVisibility(View.VISIBLE);
                            line3choice.setVisibility(View.VISIBLE);
                            line4choice.setVisibility(View.VISIBLE);
                            line5choice.setVisibility(View.VISIBLE);
                            line6choice.setVisibility(View.VISIBLE);

                            choice1Layout.setVisibility(View.VISIBLE);
                            choice2Layout.setVisibility(View.VISIBLE);
                            choice3Layout.setVisibility(View.VISIBLE);
                            choice4Layout.setVisibility(View.VISIBLE);
                            choice5Layout.setVisibility(View.VISIBLE);
                            choice6Layout.setVisibility(View.GONE);
                        }
                    }
                }

上面的代码是我更新Ui的地方。这里db立即更新。但Ui没有相应更新。我在自定义适配器的bindView()中更新了Ui。

2 个答案:

答案 0 :(得分:1)

您需要从UI线程创建并执行AsyncTask。您是在AsyncTask的doInBackground()上创建的,它可以在非UI线程上运行。将其更改为

runOnUiThread(new Runnable() {
   @Override
   public void run() {
      PollReplyAsyncTask asyncTask = new PollReplyAsyncTask(activity, false, messageThread, messageThread.getUserId());
      asyncTask.execute();
   }
}

<强>为什么吗

来自线程规则下的文档http://developer.android.com/reference/android/os/AsyncTask.html

  
      
  • 必须在UI线程上加载AsyncTask类。这是从JELLY_BEAN开始自动完成的。

  •   
  • 必须在UI线程上创建任务实例。

  •   必须在UI线程上调用
  • execute(Params ...)。

  •   
  • 不要手动调用onPreExecute(),onPostExecute(Result),doInBackground(Params ...),onProgressUpdate(Progress ...)。

  •   
  • 该任务只能执行一次(如果尝试第二次执行,则会抛出异常。)

  •   

答案 1 :(得分:0)

更多细节本来会更好。

  1. 如果您的第二个异步任务有很多事情要做,并且您正在使用onPostExecute()方法更新UI,那么在任务完成之前,UI显然不会更新。

    < / LI>
  2. 您正在第二个异步任务中执行另一个异步任务。如果您正在进行任何长时间的操作,这也可能是问题。

  3. 因此,解决方案是您可以在加密和更新数据时显示进度条,或者您可以先更新UI,然后加密并上传数据。