package com.example.threadcomm;
創(chuàng)新互聯(lián)2013年至今,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務公司,擁有項目成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元安塞做網(wǎng)站,已為上家服務,為安塞各地企業(yè)和個人服務,聯(lián)系電話:028-86922220
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. */
// 主線程給子線程發(fā)送消息
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);
// 創(chuàng)建子線程
MyThread thread = new MyThread();
thread.start();
}
OnClickListener clickListener = new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
// 主線程給子線程發(fā)送消息
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();
// 創(chuàng)建該線程的Looper對象,用于接收消息
Looper.prepare();
// 線程的looper創(chuàng)建的handler表示消息接收者是子線程
subThreadHandler = new SubThreadHandler(Looper.myLooper());
// 循環(huán)從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>
新聞名稱:AndroidUI主進程跟子進程直接相互通信
本文URL:http://muchs.cn/article36/jehhpg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、軟件開發(fā)、微信公眾號、品牌網(wǎng)站設(shè)計、網(wǎng)站制作、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)