Android异步消息处理实例

憋说话,看代码

package xxxxxx


import android.os.HandlerThread;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.view.View;
import android.util.Log;

import android.os.Handler;;
import android.os.Message;


public class MainActivity extends AppCompatActivity {

    private Button mButton;
    private final String TAG="MessageTest";
    private int ButtonCount = 0;
    private Thread myThread;
    private MyThread myThread2;
    private Handler mHandler;
    private Handler mHandler3;
    private int mMessageCount = 0;
    private HandlerThread myThread3;

    class MyRunnable implements Runnable{
        public void run(){
            int count = 0;
            for(;;){
                Log.d(TAG, "Mythread "+count);
                count++;
                try{
                    Thread.sleep(3000);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
        }
    }

    class MyThread extends Thread{
        private Looper mLooper;
        public void run(){
            super.run();
            Looper.prepare();
            synchronized (this){
                mLooper = Looper.myLooper();
                notifyAll();
            }
            Looper.loop();
        }

        public Looper getLooper(){
            if(!isAlive()){
                return null;
            }

            synchronized (this){
                while(isAlive() && mLooper == null){
                    try{
                        wait();
                    }catch (InterruptedException e){
                    }
                }

            }
            return mLooper;
        }
    }

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

        mButton = (Button)findViewById(R.id.button);
        mButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click
                Log.d(TAG, "Send Message "+ ButtonCount);
                ButtonCount++;

                Message msg = new Message();
                mHandler.sendMessage(msg);

                mHandler3.post(new Runnable() {
                    @Override
                    public void run() {
                        Log.d(TAG, "get Message for Thread3 "+ mMessageCount);
                        mMessageCount++;
                    }
                });
            }
        });

        myThread = new Thread(new MyRunnable(),"MessageTestThread");
        myThread.start();

        myThread2 = new MyThread();
        myThread2.start();

        mHandler = new Handler(myThread2.getLooper(),new Handler.Callback(){
            public boolean handleMessage(Message msg){
                Log.d(TAG,"get Message " + mMessageCount);
                mMessageCount++;
                return false;
            }
        });

        myThread3 = new HandlerThread("MessageTestThread3");
        myThread3.start();
        mHandler3 = new Handler(myThread3.getLooper());

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值