其他分享
首页 > 其他分享> > Android——runUiOnThread( Runable runable )

Android——runUiOnThread( Runable runable )

作者:互联网

介绍

  Android子线程不能操作UI组件,Google提供runUiOnThread( Runable runable )方法在子线程委托主线程更新UI组件

 

方法

  void runUiOnThread( Runable runable )

    参数:

      Runable runable:ui主线程

    返回值:无

    返回值意义:无

    作用:在子线程中调用出主线 

         

 Thread tht = new Thread(new Runnable() {
            @Override
            public void run() {
                Request request = new Request.Builder().url("http://106.55.50.79:8080/CoolWeather/china").build();
                OkHttpClient okHttpClient = new OkHttpClient();
                Call call = okHttpClient.newCall(request);
                String string = null;
                try {
                    Response response = call.execute();
                    string = response.body().string();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                String finalString = string;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Log.i("MA", "run: ");
                        txt.setText(finalString);
                    }
                });
            }
        });
        tht.start();

 

标签:Runable,runable,run,string,线程,new,Android
来源: https://www.cnblogs.com/remix777/p/15139797.html