<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>

新聞中心

EEPW首頁(yè) > 嵌入式系統 > 設計應用 > android Service中多線(xiàn)程交互

android Service中多線(xiàn)程交互

作者: 時(shí)間:2016-09-12 來(lái)源:網(wǎng)絡(luò ) 收藏

android 的service和activity是運行在UI主線(xiàn)程的。在android線(xiàn)程中,只有主線(xiàn)程即UI線(xiàn)程有自己的默認的消息隊列。子線(xiàn)程需要創(chuàng )建自己的消息隊列,并把消息發(fā)給隊列,并循環(huán)起來(lái),發(fā)給handler處理。

本文引用地址:http://dyxdggzs.com/article/201609/304411.htm

1、Looper.prepare();給子線(xiàn)程創(chuàng )建消息隊列。

2、Looper.loop();把消息放入消息隊列并循環(huán)起來(lái)。

如下是一個(gè)通過(guò)activity的oncreate()方法啟動(dòng)服務(wù),在服務(wù)里開(kāi)啟子線(xiàn)程,并發(fā)消息給主線(xiàn)程來(lái)處理的DEMO。

3、MainActivity.java如下:

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

startService(new Intent(MainActivity.this,ServiceTest.class));

}

4、ServiceTest.java

public class ServiceTest extends Service {

private Handler mHandler=new Handler(){

@Override

public void handleMessage(Message msg) {

switch (msg.what) {

case 1:

new Thread(){

@Override

public void run() {

Log.i(服務(wù), 第2個(gè)線(xiàn)程);

Looper.prepare();

for(int i=10;i20;i++){

Toast.makeText(getApplicationContext(), i+,0).show();

try {

//Thread.sleep(1000);

} catch (Exception e) {

// TODO: handle exception

}

}

mHandler.sendEmptyMessage(2);

Looper.loop();

};

}.start();

break;

case 2:

new Thread(){

@Override

public void run() {

Log.i(服務(wù), 第3個(gè)線(xiàn)程);

Looper.prepare();

for(int i=20;i30;i++){

Toast.makeText(getApplicationContext(), i+,0).show();

try {

//Thread.sleep(1000);

} catch (Exception e) {

// TODO: handle exception

}

}

mHandler.sendEmptyMessage(3);

Looper.loop();

};

}.start();

break;

case 3:

onDestroy();

break;

default:

break;

}

super.handleMessage(msg);

}

};

public ServiceTest() {

// TODO Auto-generated constructor stub

}

@Override

public IBinder onBind(Intent intent) {

// TODO Auto-generated method stub

return null;

}

@Override

public void onCreate() {

Log.i(服務(wù), onCreate());

super.onCreate();

new Thread(){

@Override

public void run() {

Log.i(服務(wù), 第一個(gè)線(xiàn)程);

Looper.prepare();

for(int i=0;i10;i++){

Toast.makeText(getApplicationContext(), i+,0).show();

try {

//Thread.sleep(1000);

} catch (Exception e) {

// TODO: handle exception

}

}

mHandler.sendEmptyMessage(1);

Looper.loop();

};

}.start();

}

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

Log.i(服務(wù), onStartCommand);

return super.onStartCommand(intent, flags, startId);

}

@Override

public void onDestroy() {

Log.i(服務(wù), onDestroy());

super.onDestroy();

stopSelf();

}

}



關(guān)鍵詞:

評論


相關(guān)推薦

技術(shù)專(zhuān)區

關(guān)閉
国产精品自在自线亚洲|国产精品无圣光一区二区|国产日产欧洲无码视频|久久久一本精品99久久K精品66|欧美人与动牲交片免费播放
<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>