android thread线程通讯

参考:http://codinghard.wordpress.com/2009/05/16/android-thread-messaging/

下载链接

源码:

package sample.thread.messaging;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

/**
 * @author davidkuo
 *
 */
public class ThreadMessaging extends Activity {
    
    private static final String TAG = "ThreadMessaging";
    private TextView mTextView;
    private Handler mMainHandler, mChildHandler;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mTextView = (TextView)findViewById(R.id.text);
        
        /*
         * Create the main handler on the main thread so it is bound to the main
         * thread's message queue. 
         */
        mMainHandler = new Handler() {
            
            public void handleMessage(Message msg) {
                
                Log.i(TAG, "Got an incoming message from the child thread - "  + (String)msg.obj);
                
                /*
                 * Handle the message coming from the child thread.
                 */
                mTextView.setText(mTextView.getText() + (String)msg.obj + "\n");
            }
        };
        
        /*
         * Start the child thread.
         */
        new ChildThread().start();
        
        Log.i(TAG, "Main handler is bound to - " + mMainHandler.getLooper().getThread().getName());
 
        Button button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                
                /*
                 * We cannot guarantee that the mChildHandler is created 
                 * in the child thread by the time the user clicks the button.
                 */
                if (mChildHandler != null) {
                    
                    /* 
                     * Send a message to the child thread.
                     */
                    Message msg = mChildHandler.obtainMessage();
                    msg.obj = mMainHandler.getLooper().getThread().getName() + " says Hello";
                    mChildHandler.sendMessage(msg);
                    Log.i(TAG, "Send a message to the child thread - " + (String)msg.obj);
                }
            }
        });
    }
    
    @Override
    protected void onDestroy() {
        
        Log.i(TAG, "Stop looping the child thread's message queue");
        
        /*
         * Remember to stop the looper
         */
        mChildHandler.getLooper().quit();
        
        super.onDestroy();
    }

    class ChildThread extends Thread {
        
        private static final String INNER_TAG = "ChildThread";
        
        public void run() {
            
            this.setName("child");
            
            /*
             * You have to prepare the looper before creating the handler.
             */
            Looper.prepare();
            
            /*
             * Create the child handler on the child thread so it is bound to the
             * child thread's message queue.
             */
            mChildHandler = new Handler() {
                
                public void handleMessage(Message msg) {
                
                    Log.i(INNER_TAG, "Got an incoming message from the main thread - " + (String)msg.obj);
                    
                    /*
                     * Do some expensive operation there. For example, you need
                     * to constantly send some data to the server.
                     */
                    try {
                        
                        /*
                         * Mocking an expensive operation. It takes 100 ms to
                         * complete.
                         */
                        sleep(100);
                        
                        /*
                         * Send the processing result back to the main thread.
                         */
                        Message toMain = mMainHandler.obtainMessage();
                        toMain.obj = "This is " + this.getLooper().getThread().getName() + 
                            ".  Did you send me \"" + (String)msg.obj + "\"?";
                        mMainHandler.sendMessage(toMain);
                        Log.i(INNER_TAG, "Send a message to the main thread - " + (String)toMain.obj);
                        
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            };
            
            Log.i(INNER_TAG, "Child handler is bound to - " + mChildHandler.getLooper().getThread().getName());
            
            /*
             * Start looping the message queue of this thread.
             */
            Looper.loop();
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值