簡體   English   中英

Android主線程做了太多的工作

[英]Android main thread is doing too much work

無論出於何種原因,該程序中的主線程都會執行過多的工作。 我不知道如何在主線程上使用視圖。 我已經嘗試過使用AsyncTask,但是我不知道該如何使用它。

Thread thread2 = new Thread(){
            public void run(){
                while(running){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            try{
                                if(red){
                                    buttons[ran].setImageResource(R.drawable.button_red);
                                    buttons[ran].setTag("Red");
                                    Thread.sleep(duration);
                                    if(buttons[ran].getTag().equals("Red")){
                                        buttons[ran].setTag("Black");
                                        buttons[ran].setImageResource(R.drawable.button_null);
                                        lifeLost();
                                        red = false;
                                    }
                                }else if(white){
                                    buttons[ran].setImageResource(R.drawable.button_white);
                                    buttons[ran].setTag("White");
                                    Thread.sleep(duration);
                                    if(buttons[ran].getTag().equals("White")){
                                        buttons[ran].setTag("Black");
                                        buttons[ran].setImageResource(R.drawable.button_null);
                                        lifeLost();
                                        white = false;
                                    }
                                }
                            }catch(Exception e){

                            }
                        }
                    });
                    try {
                        sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            }
        };
        thread2.start();

這個線程正在執行我可以給它的任何與UI相關的任務。 代碼本身實際上在此線程上工作。 但是,它僅適用於較新的設備,而較舊的設備似乎是在發現錯誤而不是忽略它。

@Override
    public void run() {
        random = new Random();
        random2 = new Random();

    try{
        Thread.sleep(1000);
        while(running){
            ran = random.nextInt(9);
            type = random2.nextInt(3);

            if(lives > 0){
                if(type == 0 || type == 2){
                  red = true;
                }else{
                    white = true;
                }

                if(lives == 0){
                        Thread.sleep(1000);
                        Intent overIntent = new Intent(this, OverActivity.class);
                        overIntent.putExtra("point", score);
                        startActivity(overIntent);
                        overridePendingTransition(0, 0);

                }

                if(score == 10){
                    duration -= 50;
                }else if(score == 20){
                    duration -= 50;
                }else if(score == 30){
                    duration -= 50;
                }else if(score == 40){
                    duration -= 50;
                }
            }
        }
    }catch(Exception e){
        e.printStackTrace();
    }
}

runOnUiThread命令中的所有內容都在UI線程上運行(顧名思義),其中包括對Thread.sleep(duration)調用(因此導致UI線程掛起/執行過多工作)。 作為簡單的第一步,請嘗試將其分成幾個runOnUiThread調用,並將Thread.sleep(duration)語句置於它們之間,而不是置於其中。

您可以創建一個類

public class DoCalculationAsyncTask extends AsyncTask<Void, Void, DataType> {

@SuppressLint("StaticFieldLeak")
private Context mContext;

public DoCalculationAsyncTask(Context context) {

}


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

    try {

    } catch (Exception e) {

        e.printStackTrace();

    } finally {

    }
    return something;
}

@Override
protected void onPostExecute(Bitmap result) {
    super.onPostExecute(result);

} }

然后這樣叫課

 try {
            DoCalculationAsyncTask doCalculationAsyncTask = new
                    DoCalculationAsyncTask(value 1, value 2, value 3, ...);
            doCalculationAsyncTask.execute();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }

暫無
暫無

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

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