美文网首页
android 子线程更新ui的几种方法

android 子线程更新ui的几种方法

作者: 湫水聊无痕 | 来源:发表于2018-03-24 13:07 被阅读0次
    package com.example.administrator.test;
    
    import android.content.Context;
    import android.os.Handler;
    import android.os.Looper;
    import android.os.Message;
    import android.support.v7.app.AlertDialog;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.ProgressBar;
    import android.widget.TextView;
    
    public class MainActivity extends AppCompatActivity {
        MyHandle handle;
        AlertDialog.Builder builder;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            handle = new MyHandle(this);
    
            Log.e("Thread-0",Thread.currentThread().getName() + '-'+Thread.currentThread().getId());
    
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Log.e("Thread-new",Thread.currentThread().getName() + '-'+Thread.currentThread().getId());
                    try {
                        // 如果在子线程中不设置延时
                        // 可以直接更新
                        // 你可以写个TEXTVIEW,在子线程中直接可以更新不会报错
                        //具体百度有说明
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
    
                    builder = new AlertDialog.Builder(MainActivity.this);
    
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
    
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            builder.setTitle("2").setMessage("2").create().show();
                            Log.e("Thread-2",Thread.currentThread().getName() + '-'+Thread.currentThread().getId());
                        }
                    });
    
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
    
                    handle.post(new Runnable() {
                        @Override
                        public void run() {
                            builder.setTitle("3").setMessage("3").create().show();
                            Log.e("Thread-3",Thread.currentThread().getName() + '-'+Thread.currentThread().getId());
                        }
                    });
    
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
    
                    handle.sendEmptyMessage(1);
    
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
    
                    Handler ui_handle = new Handler(Looper.getMainLooper());
                    ui_handle .post(new Runnable() {
                        @Override
                        public void run() {
                            builder.setTitle("6").setMessage("6").create().show();
                            Log.e("Thread-6",Thread.currentThread().getName() + '-'+Thread.currentThread().getId());
                        }
                    });
                }
            }).start();
        }
    
        private static class MyHandle extends Handler{
            Context context;
            MyHandle(Context context){
                this.context = context;
            }
    
            @Override
            public void handleMessage(Message msg) {
                MainActivity a = (MainActivity) context;
                a.builder.setTitle("4").setMessage("4").create().show();
                Log.e("Thread-4",Thread.currentThread().getName() + '-'+Thread.currentThread().getId());
                super.handleMessage(msg);
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:android 子线程更新ui的几种方法

          本文链接:https://www.haomeiwen.com/subject/jkeqcftx.html