Android 后台线程Thread调用前台线程Handler,延时线程,runOnUiThread使用,Timer延时,定时循环,倒计时
生活随笔
收集整理的這篇文章主要介紹了
Android 后台线程Thread调用前台线程Handler,延时线程,runOnUiThread使用,Timer延时,定时循环,倒计时
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
try {Thread.sleep(1000);
} catch (InterruptedException e) {e.printStackTrace();
}
?
//延時3秒 new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {public void run() {PetfunAddDevSuccess();}}, 3 * 1000); App.getInstance().runOnUiThread(new Runnable() {@Overridepublic void run() {GlideUtil.setOrginalPic(App.getGameResponse.logo,logimg);} }); SystemClock.sleep(800); App.getInstance().getUIHandler().postDelayed(dataViewPresentation::show,1000); final Timer timer =new Timer(); timer.schedule(new TimerTask() {//延時,定時循環@Overridepublic void run() {LogPlus.e("lgq","收尾。。show。。");toast.show();timer.cancel();} },2000,3000); new Timer().schedule(new TimerTask() {//倒計時@Overridepublic void run() {toast.cancel();timer.cancel();} }, cnt ); Handler handler = new Handler(); handler.postDelayed(new Runnable() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic 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(){@Overridepublic 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() {@Overridepublic void run() {activity.dismissWaitingDialog();} }); private void testSend(){new Thread(new Runnable() {@Overridepublic 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>() {@Overridepublic void accept(Long aLong) throws Exception {LogPlus.i("mqtt ======== Observable-->connect)()");// 開始連接connect();}}); } private void disposable() {if (mDisposable != null && !mDisposable.isDisposed()) {mDisposable.dispose();} }定時循環
//開始延時5秒,每次間隔12秒Observable.interval(5, 12, TimeUnit.SECONDS).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<Long>() {@Overridepublic void accept(Long aLong) {Log.d("lgqq溫度===== ","");Log.i("lgq","bbh--33--- "+packageCode());}});總結
以上是生活随笔為你收集整理的Android 后台线程Thread调用前台线程Handler,延时线程,runOnUiThread使用,Timer延时,定时循环,倒计时的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android 调用系统相机拍照和录制视
- 下一篇: Android 应用内实现导航页面,接入