Android:线程间通讯、Handler、Looper、IntentService
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
public
class
MainActivity
extends
Activity
implements
OnClickListener
{
private
Handler mHandler;
private
TextView textView;
/**
* 1.继承ItentService类的public myservice()构造方法必须存在
* 2.如果已经startservice,再次start的话,会调用onbindservice
* 3.线程深入封装:HandlerThread和IntentService
* 4.IntentService:
* class MyService extends IntentService
* {
* public MyService()
* {
* super("xxx");
* }
* onHandleMessage()
* {
* // 在"xxx"线程处理耗时操作
* }
* onBind()
* }
*/
@Override
protected
void
onCreate(Bundle savedInstanceState)
{
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button1).setOnClickListener(
this
);
findViewById(R.id.button2).setOnClickListener(
this
);
findViewById(R.id.button3).setOnClickListener(
this
);
textView = (TextView) findViewById(R.id.textView1);
}
@Override
public
boolean
onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_main, menu);
return
true
;
}
@Override
public
void
onClick(View v)
{
switch
(v.getId())
{
case
R.id.button1:
btn1Click();
break
;
case
R.id.button2:
btn2Click();
break
;
case
R.id.button3:
btn3Click();
break
;
default
:
break
;
}
}
private
void
btn3Click()
{
new
Thread(
"子线程2"
)
{
@Override
public
void
run()
{
Message msg =
new
Message();
msg.obj =
30
;
mHandler.sendMessage(msg);
}
}.start();
}
private
void
btn2Click()
//将main线程消息发送给子线程
{
Message msg =
new
Message();
msg.arg2 =
20
;
mHandler.sendMessage(msg);
}
private
void
btn1Click()
//启动自定义消息队列
{
try
{
Thread.sleep(
5000
);
}
catch
(InterruptedException e)
{
e.printStackTrace();
}
new
Thread(
"子线程"
)
{
@Override
public
void
run()
{
Looper.prepare();
//创建Looper和MessageQueue两个对象
mHandler =
new
Handler()
//由Looper决定哪个线程来执行Handler
{
public
void
handleMessage(android.os.Message msg)
{
String threadname = Thread.currentThread().getName();
Log.e(
"handler"
,
"threadname:"
+ threadname +
" msg_1:"
+ msg.arg1 +
" msg_main:"
+ msg.arg2 +
" msg_2:"
+ msg.obj);
//可以接收到消息,自己给自己发送消息
};
};
Message msg =
new
Message();
msg.arg1 =
10
;
mHandler.sendMessage(msg);
// mHandler.sendEmptyMessage(1);
Looper.loop();
//在该线程中运行消息
}
}.start();
}
}
|
注:
1.结束子线程的方法:设置一个flag标记进行判断
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
private
void
saveCityInfo(
final
HttpHelperResponse response)
{
new
Thread(
new
Runnable()
{
@Override
public
void
run()
{
while
(!isThreadOver)
{
Looper.prepare();
CityInfoList cityInfo = HBJsonUtils.parseAllCityInfoJson(response.getMsgStr());
// 解析
// 将下载结果写入缓存
mDBHelper.addToCache(cityInfo, mCityId);
// 释放
recycleCityInfoList(cityInfo);
handler.sendEmptyMessage(MSG_SAVE_CITYINFO);
Looper.loop();
}
}
}).start();
}
|
本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1210685,如需转载请自行联系原作者