日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

活用Android的Message Queue(3)

系統(tǒng) 1975 0

3.?? 由主線程發(fā)送消息給子線程 ( 續(xù) )

???? 上述范例里,是由子線程發(fā)送消息給主線程。本節(jié)將介紹如何從主線程 發(fā)送消息 給子線程。其方法是:當(dāng)子線程執(zhí)行 run() 函數(shù)時(shí),就創(chuàng)建一個(gè)子線程的 Handler 對(duì)象。之后,當(dāng)主線程執(zhí)行 ac01 onClick() 函數(shù)時(shí),就藉由此 Handler 對(duì)象引用而 push 消息給子線程。例如下述范例:

?

//----- Looper_04 范例 ?-----

public ? class ?ac01? extends ?Activity? implements ?OnClickListener {

???? private ? final ? int ? WC ?= LinearLayout.LayoutParams. WRAP_CONTENT ;

???? private ? final ? int ? FP ?= LinearLayout.LayoutParams. FILL_PARENT ;

???? public ?TextView? tv ;

???? private ?myThread? t ;

???? private ?Button? btn ,? btn2 ;

???? private ?Handler? h ;

???? private ?Context? ctx ;

???? public ? void ?onCreate(Bundle icicle) {

???????????? super .onCreate(icicle);

???????????? ctx ?=? this ;

????????????????LinearLayout layout =? new ?LinearLayout( this );

????????????????layout.setOrientation(LinearLayout. VERTICAL );

??????????????????????????????

???????????????? btn ?=? new ?Button( this );

???????????????? btn .setId(101);

???????????????? btn .setBackgroundResource(R.drawable. heart );

???????????????? btn .setText( "test looper" );

???????????????? btn .setOnClickListener( this );

????????????????LinearLayout.LayoutParams param =

???????????????????? new ?LinearLayout.LayoutParams(100,50);

????????????????param. topMargin ?= 10;

????????????????layout.addView( btn , param);

???????????????

???????????????? btn2 ?=? new ?Button( this );

???????????????? btn2 .setId(102);

???????????????? btn2 .setBackgroundResource(R.drawable. ok_blue );

???????????????? btn2 .setText( "exit" );

???????????????? btn2 .setOnClickListener( this );

????????????????layout.addView( btn2 , param);

???????????????

???????????????? tv ?=? new ?TextView( this );

???????????????? tv .setTextColor(Color. WHITE );

???????????????? tv .setText( "" );

????????????????LinearLayout.LayoutParams param2 =

??????????????????? new ?LinearLayout.LayoutParams( FP ,? WC );

????????????????param2. topMargin ?= 10;

????????????????layout.addView( tv , param2);

????????????????setContentView(layout);?

???????????????? //------------------------

???????????????? t ?=? new ?myThread();

????????????????? t .start();

??????????}

?????????? public ? void ?onClick(View v) {

???????? switch (v.getId()){

???????? case ?101:

?????????????String obj =? "mainThread" ;

?????????????Message m =? h .obtainMessage(1, 1, 1, obj);

????????????? h .sendMessage(m);

????????????? break ;

???????? case ?102:

???????????? h .getLooper().quit();

????????finish();

???????????? break ;

????????}

????}

//------------------------------------------------?????

public ? class ?EventHandler? extends ?Handler {

?????????? public ?EventHandler(Looper looper) {

?????????????????????? super (looper);

??????????}

??????????? @Override

?????????????? public ? void ?handleMessage(Message msg) {

?????????????????????((Activity) ctx ).setTitle((String)msg. obj );

????????????}

????????}

//------------------------------------------------?????

class ?myThread? extends ?Thread{

????? public ? void ?run() {

?????????Looper. prepare ();

????????? h ?=? new ?Handler(){

??????????????? public ? void ?handleMessage(Message msg) {

??????????????????EventHandler ha =? new

?????????????????????EventHandler(Looper. getMainLooper ());

????????????????????String obj = (String)msg. obj ?+? ", myThread" ;

?????????????????????Message m = ha.obtainMessage(1, 1, 1, obj);

?????????????????????ha.sendMessage(m);

?????????}

?????????};

?????????Looper. loop ();

??????}

??}

}

?

?

當(dāng)子線程執(zhí)行 run() 函數(shù)時(shí),創(chuàng)建一個(gè)主線程的 EventHandler 對(duì)象,并且藉之而 push 消息給主線程了。就進(jìn)行了兩個(gè)線程之間的互相交換消息,也是兩個(gè)函數(shù)或?qū)ο箝g之交換消息。此程序輸出畫面為:

?

?

2

?

????? 上述范例定義了 Thread 的子類別。也可以將子線程包含到 Runnable 類別里,如下:

?

//----- Looper_04aa 范例 ?-----

public ? class ?ac01? extends ?Activity? implements ?OnClickListener {

???? private ? final ? int ? WC ?= LinearLayout.LayoutParams. WRAP_CONTENT ;

???? private ? final ? int ? FP ?= LinearLayout.LayoutParams. FILL_PARENT ;

???? public ?TextView? tv ;

???? private ?RR? r ;

???? private ?Button? btn ,? btn2 ;

???? private ?Handler? h ;

???? private ?Context? ctx ;

???? public ? void ?onCreate(Bundle icicle) {

???????????? super .onCreate(icicle);

???????????? ctx ?=? this ;

????????????????LinearLayout layout =? new ?LinearLayout( this );

????????????????layout.setOrientation(LinearLayout. VERTICAL );

??????????????????????????????

???????????????? btn ?=? new ?Button( this );

???????????????? btn .setId(101);

???????????????? btn .setBackgroundResource(R.drawable. heart );

???????????????? btn .setText( "test looper" );

???????????????? btn .setOnClickListener( this );

????????????????LinearLayout.LayoutParams param =

???????????????????? new ?LinearLayout.LayoutParams(100,50);

????????????????param. topMargin ?= 10;

????????????????layout.addView( btn , param);

???????????????

???????????????? btn2 ?=? new ?Button( this );

???????????????? btn2 .setId(102);

???????????????? btn2 .setBackgroundResource(R.drawable. ok_blue );

???????????????? btn2 .setText( "exit" );

???????????????? btn2 .setOnClickListener( this );

????????????????layout.addView( btn2 , param);

???????????????

???????????????? tv ?=? new ?TextView( this );

???????????????? tv .setTextColor(Color. WHITE );

???????????????? tv .setText( "" );

????????????????LinearLayout.LayoutParams param2 =

??????????????????? new ?LinearLayout.LayoutParams( FP ,? WC );

????????????????param2. topMargin ?= 10;

????????????????layout.addView( tv , param2);

????????????????setContentView(layout);?

???????????????? //------------------------

???????????????? r ?=? new ?RR();

?????????}

?????????? public ? void ?onClick(View v) {

???????? switch (v.getId()){

???????? case ?101:

?????????????String obj =? "mainThread" ;

?????????????Message m =? h .obtainMessage(1, 1, 1, obj);

????????????? h .sendMessage(m);

????????????? break ;

???????? case ?102:

???????????? h .getLooper().quit();

????????finish();

???????????? break ;

????????}

????}

//------------------------------------------------??????

public ? class ?EventHandler? extends ?Handler {

?????????? public ?EventHandler(Looper looper) {

?????????????????????? super (looper);

??????????}

??????????? @Override

?????????????? public ? void ?handleMessage(Message msg) {

?????????????????????((Activity) ctx ).setTitle((String)msg. obj );

????????????}

????????}

//------------------------------------------------?????

public ? class ?RR? implements ?Runnable {

???? public ?RR() {

????????Thread aThread =? new ?Thread( null ,? this ,? "RR" );

????????aThread.start();

????}

???? public ? void ?run() {

?????????Looper. prepare ();

????????? h ?=? new ?Handler(){

??????????????? public ? void ?handleMessage(Message msg) {

??????????????????EventHandler ha =? new ?EventHandler(Looper. getMainLooper ());

????????????????????String obj = (String)msg. obj ?+? ", myThread" ;

?????????????????????Message m = ha.obtainMessage(1, 1, 1, obj);

?????????????????????ha.sendMessage(m);

?????????}

?????????};

?????????Looper. loop ();

??????}

??}

}

?

?

當(dāng)子線程執(zhí)行到 RR() 函數(shù)時(shí),創(chuàng)建一個(gè)子線程,并執(zhí)行 run() 函數(shù),就將 消息發(fā)送 給主線程了。

活用Android的Message Queue(3)


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 潮安县| 金阳县| 杂多县| 宁乡县| 平江县| 无极县| 辽宁省| 庆阳市| 五指山市| 堆龙德庆县| 茌平县| 五莲县| 太原市| 洛浦县| 东乌| 沙坪坝区| 中山市| 荃湾区| 周至县| 万荣县| 芒康县| 遂川县| 抚宁县| 锡林浩特市| 涟源市| 托克逊县| 泸定县| 西盟| 探索| 呼伦贝尔市| 竹北市| 前郭尔| 尖扎县| 哈密市| 无极县| 修文县| 礼泉县| 灌阳县| 舞钢市| 南宫市| 安岳县|