android 主线程与分线程 做同步

直接上代码

package com.example.handlerthreaddemo;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {
    Test t;
    Handler mh=new Handler(){

        @Override
        public void handleMessage(Message msg) {
            String s = (String) msg.obj;
            Bundle data = msg.getData();
            String title = data.getString("title");
            String info = data.getString("info");
            System.out.println("title is " + title + ", info is"
                    + info);
            System.out.println("msg.obj is " + s);
            System.out.println("handlerMessage() is "+Thread.currentThread().getId());
        }
        
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        t=new Test(mh);
        
    }
    public void OnClick(View v){
        switch (v.getId()) {
        case R.id.button1:
            t.send();
            break;

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


分线程代码:

package com.example.handlerthreaddemo;

import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;

public class Test {
    HandlerThread mhandlerThread1, mhandlerThread2;
    Handler mhandler1;
    Handler mhandler2;

    public Test(Handler mh) {
        mhandler2=mh;
        mhandlerThread1 = new HandlerThread("update");
        mhandlerThread1.setDaemon(true);
        mhandlerThread1.start();
        mhandler1 = new Handler(mhandlerThread1.getLooper()) {

            @Override
            public void handleMessage(Message msg) {
                String s = (String) msg.obj;
                Bundle data = msg.getData();
                String title = data.getString("title");
                String info = data.getString("info");
                System.out.println("title is " + title + ", info is"
                        + info);
                System.out.println("msg.obj is " + s);
                System.out.println("handlerMessage() is "+Thread.currentThread().getId());
                
                //----------------------------
                Message msg1 = new Message();

                msg1.obj = "abc";

                Bundle data1 = new Bundle();

                data1.putString("title", "你好吗");

                data1.putString("info", "哈哈哈哈");

                msg1.setData(data1);
                mhandler2.sendMessage(msg1);
            }
        };
    }

    public void send() {
        System.out.println("send() is "+Thread.currentThread().getId());
        Message msg = new Message();

        msg.obj = "abc";

        Bundle data = new Bundle();

        data.putString("title", "你好吗");

        data.putString("info", "哈哈哈哈");

        msg.setData(data);
        mhandler1.sendMessage(msg);
        
        // 将msg发送到对象,所谓的目标对象就是生成该msg对象的Handler对象

    }
}

xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/textView1"
        android:layout_marginRight="16dp"
        android:layout_marginTop="44dp"
        android:text="Button"
        android:onClick="OnClick" />

</RelativeLayout>

csdn下载路径:http://download.csdn.net/detail/wenwei19861106/4849987

posted on 2012-12-05 16:48  南瓜饼  阅读(494)  评论(0编辑  收藏  举报

导航