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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值