Android延时的功能,在很多的地方都有用,这里结合网上看到的一些资料,总结一下,
1. 最简单的方法,就是直接呼叫“Thread.sleep()”
注意几个问题:
a。需要加入try ... catch机制;
b。sleep时间过长,容易造成APK异常,系统提示没有反应;
参考代码如下,
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button01:
try {
Thread.sleep(2 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Toast.makeText(this, "Hello World", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
2. 使用“Handler().postDelayed”延时,原先猜测会新开thread,经过测试,和主程序的thread ID一样,
测试代码如下,
private void delay1 (int delaytime) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Log.v(TAG, String.valueOf(android.os.Process.myPid()));
Log.v(TAG, String.valueOf(android.os.Process.myTid()));
Toast.makeText(com.wistron.test02.MainActivity.this, "dealy1.....", Toast.LENGTH_SHORT).show();
}
}, delaytime);
}
3. 新开一个Thead,使用Thread.sleep()。新开的thread ID和主程序的不一样,这在预料之中。
测试代码如下,
private void delay2 (final int delayTime) {
new Thread(new Runnable() {
@Override
public void run() {
try{
Thread.sleep(delayTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.v(TAG, String.valueOf(android.os.Process.myPid()));
Log.v(TAG, String.valueOf(android.os.Process.myTid()));
Toast.makeText(com.wistron.test02.MainActivity.this, "dealy2.....", Toast.LENGTH_SHORT).show();
}
}).start();
}
4. 使用TimerTask和Timer,新的thread ID和主程序的thread ID不一样。
测试代码如下,
private void delay3(int delayTime) {
TimerTask task = new TimerTask() {
@Override
public void run() {
Log.v(TAG, String.valueOf(android.os.Process.myPid()));
Log.v(TAG, String.valueOf(android.os.Process.myTid()));
Toast.makeText(com.wistron.test02.MainActivity.this, "dealy3.....", Toast.LENGTH_SHORT).show();
}
};
Timer timer = new Timer();
timer.schedule(task, delayTime);
}
5. 使用“handler.sendMessageDelayed”,thread ID和主程序一样。参考代码如下,其余代码,请参考文末的完整代码。
private void delay4 (int delayTime) {
Log.v(TAG, "delay4: " + String.valueOf(android.os.Process.myPid()));
Log.v(TAG, "delay4: " + String.valueOf(android.os.Process.myTid()));
Message msg = new Message();
msg.what = 104;
handler.sendMessageDelayed(msg, delayTime);
}
好了,这次的基础知识就分享怎么多,下面呈现完整的测试代码,
package com.xxx.test02;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.webkit.ConsoleMessage;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private static final String TAG = "TestDelay";
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.v(TAG, "Main Program: " + String.valueOf(android.os.Process.myPid()));
Log.v(TAG, "Main Program: " + String.valueOf(android.os.Process.myTid()));
Button button01 = (Button) findViewById(R.id.button01);
button01.setOnClickListener(this);
final TextView text = findViewById(R.id.text01);
handler = new Handler(){
public void handleMessage(Message msg) {
Log.v(TAG, "Handler: " + String.valueOf(android.os.Process.myPid()));
Log.v(TAG, "Handler: " + String.valueOf(android.os.Process.myTid()));
if (msg.what == 100){
text.setText("Time0: " + getTime());
}
if (msg.what == 101) {
text.setText("Time1: " + getTime());
}
if (msg.what == 102) {
text.setText("Time2: " + getTime());
}
if (msg.what == 103) {
text.setText("Time3: " + getTime());
}
if (msg.what == 104) {
text.setText("Time4: " + getTime());
}
}
};
}
private String getTime() {
long time = System.currentTimeMillis();
Date date = new Date(time);
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
return format.format(date);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button01:
// TO-DO
/*try {
Thread.sleep(20 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Toast.makeText(this, "Hello World", Toast.LENGTH_SHORT).show();*/
delay4(2*1000);
break;
default:
break;
}
}
private void delay1 (int delaytime) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Log.v(TAG, "delay1: " + String.valueOf(android.os.Process.myPid()));
Log.v(TAG, "delay1: " + String.valueOf(android.os.Process.myTid()));
Toast.makeText(com.wistron.test02.MainActivity.this, "dealy1.....", Toast.LENGTH_SHORT).show();
}
}, delaytime);
}
private void delay2 (final int delayTime) {
new Thread(new Runnable() {
@Override
public void run() {
try{
Thread.sleep(delayTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
Message msg = new Message();
msg.what = 102;
handler.sendMessage(msg);
Log.v(TAG, "delay2: " + String.valueOf(android.os.Process.myPid()));
Log.v(TAG, "delay2: " + String.valueOf(android.os.Process.myTid()));
}
}).start();
}
private void delay3(int delayTime) {
TimerTask task = new TimerTask() {
@Override
public void run() {
Message msg = new Message();
msg.what = 103;
handler.sendMessage(msg);
Log.v(TAG, "delay3: " + String.valueOf(android.os.Process.myPid()));
Log.v(TAG, "delay3: " + String.valueOf(android.os.Process.myTid()));
}
};
Timer timer = new Timer();
timer.schedule(task, delayTime);
}
private void delay4 (int delayTime) {
Log.v(TAG, "delay4: " + String.valueOf(android.os.Process.myPid()));
Log.v(TAG, "delay4: " + String.valueOf(android.os.Process.myTid()));
Message msg = new Message();
msg.what = 104;
handler.sendMessageDelayed(msg, delayTime);
}
}