Android UI主進程跟子進程直接相互連信

來源:互聯網
上載者:User

標籤:android 處理序間通訊

package com.example.threadcomm;

import android.R.integer;
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;

public class MainActivity extends Activity {
 /** Called when the activity is first created. */
 // 主線程給子線程發送訊息
 Button verifyButton;
 TextView textView;

 private static int FROM_MAIN_MSG1 = 1;
 private static int FROM_SUB_MSG2 = 11;

 // 主線程
 MainActivityHandler mainHandler;
 // 其它線程
 SubThreadHandler subThreadHandler;

 private int i = 0;

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

  verifyButton = (Button) findViewById(R.id.verify_button);
  textView = (TextView) findViewById(R.id.text);

  textView.setText("");

  verifyButton.setOnClickListener(clickListener);

  // 建立子線程
  MyThread thread = new MyThread();
  thread.start();
 }

 OnClickListener clickListener = new OnClickListener() {

  @Override
  public void onClick(View v) {
   switch (v.getId()) {
   // 主線程給子線程發送訊息
   case R.id.verify_button:
    textView.append("UI main thread: shall send message to sub-thread......\n\n");
    subThreadHandler.sendEmptyMessage(1);
    break;

   default:
    break;
   }
  }
 };

 // 主線程處理訊息

 class MainActivityHandler extends Handler {

  public MainActivityHandler(Looper looper) {
   super(looper);
  }

  @Override
  public void handleMessage(Message msg) {
   super.handleMessage(msg);

   if (msg.what == 11) {
    textView.append("UI main thread: received message from sub-thread......\n\n");
   }
  }
 }

 // 以下是子線程類和他的handler類
 class MyThread extends Thread {
  @Override
  public void run() {
   Log.e("threads_comm", "sub thread was created here......");

   super.run();
   // 建立該線程的Looper對象,用於接收訊息
   Looper.prepare();
   // 線程的looper建立的handler表示訊息接收者是子線程
   subThreadHandler = new SubThreadHandler(Looper.myLooper());
   // 迴圈從MessageQueue中取訊息。
   Looper.loop();
  }
 }

 // 子線程的Handler

 class SubThreadHandler extends Handler {
  public SubThreadHandler(Looper looper) {
   super(looper);
  }

  @Override
  public void handleMessage(Message msg) {
   super.handleMessage(msg);

   if (msg.what == FROM_MAIN_MSG1) {
    mainHandler = new MainActivityHandler(Looper.getMainLooper());
    textView.append("Sub-thread: received message from UI main thread, and will send message to UI main thread......\n\n");
    mainHandler.sendEmptyMessage(11);

   }
  }
 }
}



對應的activity_main.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="${relativePackage}.${activityClass}" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:layout_above="@+id/button3"
        android:layout_marginBottom="50dp"
        />

    <Button
        android:id="@+id/verify_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="send"
        android:layout_centerInParent="true"
        android:background="@drawable/shape"
        />
</RelativeLayout>


Android UI主進程跟子進程直接相互連信

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.