android利用Handler开启线程和关闭线程

android利用Handler开启线程和关闭线程

直接上代码:

private Handler mHandler=new Handler();
private String mPassword;
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        mPassword="";
        if (!TextUtils.isEmpty(mPassword)) {
            mHandler.postDelayed(gotoMainAct, 2000);
        } else {
            mHandler.postDelayed(gotoLoginAct, 2000);
        }
    }


Runnable gotoLoginAct = new Runnable() {
        @Override
        public void run() {
            Log.e("gotoLoginAct ");
            mHandler.removeCallbacks(gotoLoginAct);//停止线程
        }
    };

Runnable gotoMainAct = new Runnable() {
    @Override
    public void run() {
        Log.e("gotoMainAct ");
        mHandler.removeCallbacks(gotoMainAct );//停止线程   
    }
};

你可能感兴趣的:(android)