Android 常见的延时执行的方法

1.使用线程的休眠实现延时操作

public class MainActivity extends AppCompatActivity {
    private TextView tv;
    private Button bt;
    public static final int UPDATA = 0x1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = findViewById(R.id.textView);
        bt = findViewById(R.id.button);

        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(){
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(3000);
                            Message message = new Message();
                            message.what = UPDATA;
                            message.arg1 = 1000;
                            handler.sendMessage(message);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
            }
        });
    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case UPDATA:
                    tv.setText(String.valueOf(msg.arg1));
                    break;

            }
        }
    };

2.使用TimerTask实现延时操作

public class MainActivity extends AppCompatActivity {
    private TextView tv;
    private Button bt;
    public static final int UPDATA = 0x1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = findViewById(R.id.textView);
        bt = findViewById(R.id.button);

        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TimerTask task = new TimerTask() {
                    @Override
                    public void run() {
                        /**
                         *要执行的操作
                         */
                        
                        Message message = new Message();
                        message.what = UPDATA;
                        message.arg1 = 1000;
                        handler.sendMessage(message);
                    }
                };
                Timer timer = new Timer();
                timer.schedule(task, 3000);//3秒后执行TimeTaskrun方法

            }
        });
    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case UPDATA:
                    tv.setText(String.valueOf(msg.arg1));
                    break;

            }
        }
    };

}

3.使用Handler的postDelayed方法实现延时操作

public class MainActivity extends AppCompatActivity {
    private TextView tv;
    private Button bt;
    public static final int UPDATA = 0x1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = findViewById(R.id.textView);
        bt = findViewById(R.id.button);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        tv.setText("3000");
                    }
                },3000);
            }
        });


    }

}
  你可以自己看线程 , 怎么来执行是好的 ,这样最合适的方式;



你可能感兴趣的:(Android 常见的延时执行的方法)