簡體   English   中英

runOnUi 減慢應用程序並使其強制關閉

[英]runOnUi slows down the app and make it forced close

我正在無限時間內生成一個隨機字符串並將其設置為 EditText。 當我不使用 runOnUi 應用程序時,它正在處理具有高性能的較新設備。 但是當我啟動線程並給出錯誤(從錯誤的線程異常調用)時,它會在舊模型上崩潰

然后我使用了 runOnUi 但它使超級慢並強制關閉它。

Thread thread = new Thread(new Runnable() {
                   @Override
                   public void run() {
                       while (true) {
                           runOnUiThread(new Runnable() {
                               @Override
                               public void run() {
                                   try {
                                       tryPass.setText(getAlphaNumericString());
                                       Thread.sleep(2000);
                                   } catch (InterruptedException e) {
                                       e.printStackTrace();
                                   }


                               }
                           });
                       }
                   }
               });
               thread.start();

您試圖通過調用Thread.sleep(2000);來阻止 UI 線程Thread.sleep(2000); 在 UI 線程上。

試試這個方法:

Thread thread = new Thread(new Runnable() {
    @Override
    public void run() {
        while (true) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    tryPass.setText(getAlphaNumericString());
                }
            });
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
});
thread.start();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM