android 主線程與分線程 做同步

來源:互聯網
上載者:User

直接上代碼

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.