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

【达内课程】线程——Handler(三)

程序员文章站 2022-05-14 17:53:55
...

【达内课程】线程——Handler(三)

实现3s后跳转主界面

【达内课程】线程——Handler(三)

主要代码:

SplashActivity

public class SplashActivity extends AppCompatActivity {
    private Handler handler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                Intent intent = new Intent(SplashActivity.this,MainActivity.class);
                startActivity(intent);
            }
        };
        handler.sendEmptyMessageDelayed(0,3000);
    }
}

【达内课程】线程——Handler(三)
【达内课程】线程——Handler(三)

现在用方式2来做上一节进度条栗子

public class SecondActivity extends AppCompatActivity implements View.OnClickListener {
    private ProgressBar progressBar;
    private Button button;
    private TextView textView;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        button = (Button) findViewById(R.id.button);
        textView = (TextView) findViewById(R.id.tv_show_progress);
        button.setOnClickListener(this);

        InnerHandlerCallback innerHandlerCallback = new InnerHandlerCallback();
        handler = new Handler(innerHandlerCallback);
    }

    @Override
    public void onClick(View view) {
        InnerThread thread = new InnerThread();
        thread.start();
    }

    private class InnerHandlerCallback implements Handler.Callback{

        @Override
        public boolean handleMessage(Message msg) {
            if(msg.what == MESSAGE_UPDATE_PROGRESS) {
                Log.d("Handler","InnerHandlerCallback.handleMessage->"+MESSAGE_UPDATE_PROGRESS);
                progressBar.setProgress(msg.arg1);
                textView.setText(msg.arg1 + "/100");
            }else if(msg.what == MESSAGE_UPDATE_COMPLETE){
                Log.d("Handler","InnerHandlerCallback.handleMessage->"+MESSAGE_UPDATE_COMPLETE);
                Toast.makeText(SecondActivity.this,"更新进度完成",Toast.LENGTH_SHORT).show();
            }
            return false;
        }
    }

    public static final int MESSAGE_UPDATE_PROGRESS = 999;
    public static final int MESSAGE_UPDATE_COMPLETE = 998;
    private class InnerThread extends Thread{
        @Override
        public void run() {
            for(int i=0;i<=100;i++){
                //发消息,通知主线程更新UI
                Message msg = Message.obtain();
                msg.what = MESSAGE_UPDATE_PROGRESS;
                msg.arg1 = i;
                handler.sendMessage(msg);
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            //发出消息,标识更新进度完成
            Message msg = Message.obtain();
            msg.what = MESSAGE_UPDATE_COMPLETE;
            handler.sendMessage(msg);
        }
    }
}

如果InnerHandler增加一个构造方法,代码修改为

 private class InnerHandler extends Handler{
        public InnerHandler(Callback callback) {
            super(callback);
        }

        @Override
        public void handleMessage(Message msg) {
            if(msg.what == MESSAGE_UPDATE_PROGRESS) {
                Log.d("Handler","InnerHandler.handleMessage->"+MESSAGE_UPDATE_PROGRESS);
                progressBar.setProgress(msg.arg1);
                textView.setText(msg.arg1 + "/100");
            }else if(msg.what == MESSAGE_UPDATE_COMPLETE){
                Log.d("Handler","InnerHandler.handleMessage->"+MESSAGE_UPDATE_COMPLETE);
                Toast.makeText(SecondActivity.this,"更新进度完成",Toast.LENGTH_SHORT).show();
            }
        }
    }

在onCreate中

InnerHandlerCallback innerHandlerCallback = new InnerHandlerCallback();
        handler = new InnerHandler(innerHandlerCallback);

这两种方式同时存在,那么

InnerHandlerCallback.handleMessage->999
InnerHandler.handleMessage->999
......
InnerHandlerCallback.handleMessage->998
InnerHandler.handleMessage->998

如果

private class InnerHandlerCallback implements Handler.Callback{

        @Override
        public boolean handleMessage(Message msg) {
            ......
            }
            return true;
        }
    }
InnerHandlerCallback.handleMessage->999
InnerHandlerCallback.handleMessage->999
......
InnerHandlerCallback.handleMessage->998

方式3,反正不用……简单写

private class InnerThread extends Thread{
        @Override
        public void run() {
            for(int i=0;i<=100;i++){
                Message msg = Message.obtain(handler, new Runnable() {
                    @Override
                    public void run() {
                        Log.d("Handler", "Thread ID:"+currentThread().getId()+"Message.obtain->Runnable.run");
                    }
                });
                msg.sendToTarget();
                //发消息,通知主线程更新UI
               /* Message msg = Message.obtain();
                msg.what = MESSAGE_UPDATE_PROGRESS;
                msg.arg1 = i;
                handler.sendMessage(msg);*/
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            //发出消息,标识更新进度完成
            ......
        }
    }
相关标签: handler