Android 后台线程Thread调用前台线程Handler,延时线程,runOnUiThread使用,Timer延时,定时循环,倒计时

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() {

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值