欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android在子线程更新UI主线程详情方法

程序员文章站 2022-07-03 08:08:35
最近用了一下okhttp,原本以为异步callback里面的onResponse是运行在主线程的,后面发现在onResponse里面放了Toast,半天没有反映,一度以为Toast...

最近用了一下okhttp,原本以为异步callback里面的onResponse是运行在主线程的,后面发现在onResponse里面放了Toast,半天没有反映,一度以为Toast写错了,把Toast放外面发现可以正常弹出提示的,后来查了下资料,原来okhttp的callback,onResponse其实还是运行在子线程的,那么如果在子线程更新主线程呢。这里提供几种方法。

  //定义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);
其实通过查看源代码,发现非handler的实现方法最终还是通过handler来实现的。
  LoginActivity.this.runOnUiThread(new Runnable() {});
  view.post(new Runnable() );
  //以上两个方法最终都调用handler的sendMessageDelayed方法
  public final boolean sendMessageDelayed(Message msg, long delayMillis)