需要更多色彩的
不是代码,而是生活

android在子线程更新UI主线程的六种方法

//定义handler
static final int SUCCESS = 1;
static final int FAIL = 0;
Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what) {
            case SUCCESS:
                Toast.makeText(LoginActivity.this, msg.getData().get("msg").toString(), Toast.LENGTH_SHORT).show();
                break;
            case FAIL:
                Toast.makeText(LoginActivity.this, msg.getData().get("msg").toString(), Toast.LENGTH_SHORT).show();
                break;
            default:
                super.handleMessage(msg);
        }
    }
};

//方法一:通过handler.sendMessage,更新UI主线程
private void makeToastByHandlerSendMessage(String msgStr) {
    Message msg = new Message();
    msg.what = 0;
    Bundle bundle = new Bundle();
    bundle.putString("msg", msgStr);
    msg.setData(bundle);
    handler.sendMessage(msg);
}

//方法二:通过handler.post,更新UI主线程
private void makeToastByHandlerPost(final String msg) {
    handler.post(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(LoginActivity.this, msg, Toast.LENGTH_SHORT).show();
        }
    });
}

//方法三:通过handler.post,延迟seconds秒后更新UI主线程
private void makeToastByHandlerPostDelay(final String msg, int seconds) {
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(LoginActivity.this, msg, Toast.LENGTH_SHORT).show();
        }
    }, seconds * 1000);
}

//方法四:使用runOnUiThread,更新UI主线程
private void makeToastByRunOnUiThread(final String msg) {
    LoginActivity.this.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(LoginActivity.this, msg, Toast.LENGTH_SHORT).show();
        }
    });
}

//方法五:通过view.post,更新UI主线程
private void makeToatByViewPost(View view, final String msg) {
    view.post(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(LoginActivity.this, msg, Toast.LENGTH_SHORT).show();
        }
    });
}

//方法六:通过AsyncTask,更新UI主线程
private void makeToastByAysncTask(final String msg) {
    AsyncTask asyncTask = new AsyncTask() {
        @Override
        protected Object doInBackground(Object[] params) {
            return null;
        }

        @Override
        protected void onPostExecute(Object o) {
            super.onPostExecute(o);
            Toast.makeText(LoginActivity.this, msg, Toast.LENGTH_SHORT).show();
        }

        @Override
        protected void onProgressUpdate(Object[] values) {
            super.onProgressUpdate(values);
        }
    };
    asyncTask.execute();
}

调用

//1、Handler.SendMessage
makeToastByHandlerSendMessage(msg);
//2、Handler.Post
makeToastByHandlerPost(msg);
//3、Handler.PostDelay
makeToastByHandlerPostDelay(msg,1);
//4、context.RunOnUiThread
makeToastByRunOnUiThread(msg);
//5、View.Post
makeToatByViewPost(btnLogin,msg);
//6、AysncTask
makeToastByAysncTask(msg);
赞(1) 打赏
未经允许不得转载:代码一线 » android在子线程更新UI主线程的六种方法
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

觉得文章有用就支持一下本站

支付宝扫一扫打赏

微信扫一扫打赏