handler.postDelayed(new Runnable() {
@Override
public void run() {
startActivity(PayBannerActivity.class);
setResult(RESULT_OK);
finish();
}
}, 1500);
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
public void run() {
Log.e("lgq","延时。。。");
Toast.makeText(MainActivity.this,"saa",1).show();
}
}, 3000);
调用UI线程更新UI
-----------
private void setTips(String s) {
if (this == null) {
return;
}
// runOnUiThread(() -> {
// textView.setText(s);
// });
runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText(s);
}
});
}
### 创建前台线程
static final int REFRESH_COMPLETE = 0X1112;
private Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case REFRESH_COMPLETE:
//todo…
break;
}
}
};
### 后台线程调用前台线程,循环线程
new Thread() {
@Override
public void run() {
super.run();
running = true;
int i = 0;
while (running) {
mHandler.sendEmptyMessageDelayed(REFRESH_COMPLETE, 1000);
try {
sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
new Thread() {
@Override
public void run() {
super.run();
boolean running = true;
int i = 0;
while (running) {
if (i<3){
App.synthesizer.speak("请点击启动按钮");
i++;
}
try {
sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
延时线程
====
new Thread(){
@Override
public void run() {
try {
Thread.sleep(5 * 100);
} catch (InterruptedException e) {
e.printStackTrace();
}
mHandler2=new Handler();
Log.i("Lgq","s....e33.");
}
}.start();
private BaseActivity activity;
public void setActivity(BaseActivity activity) {
this.activity = activity;
}
private Handler mHandler = new Handler();
mHandler.post(new Runnable() {
@Override
public void run() {
activity.dismissWaitingDialog();
}
});
private void testSend(){
new Thread(new Runnable() {
@Override
public void run() {
int i=20;
while (i>0){
send_0A06();
LogPlus.e("onSuccess---发送次数===" + i);
i--;
SystemClock.sleep(3000);
}
}
}).start();
}
定时延时
====
connect(5);//延时5秒
private Disposable mDisposable;
private void connect(long delay) {
disposable();
mDisposable = Observable.timer(delay, TimeUnit.SECONDS).subscribeOn(Schedulers.newThread()).subscribe(new Consumer<Long>() {
@Override
public void accept(Long aLong) throws Exception {
LogPlus.i("mqtt ======== Observable-->connect)()");
// 开始连接
connect();
}
});
}
private void disposable() {