易网时代-编程资源站
Welcome
微信登录
首页
/
操作系统
/
Linux
/
对Android中handler处理两个线程的理解
我看了网上的教程是这样的,用handler除了一个线程,就是控制progressbar。于是我拓展了一下,我另外写了个计数的线程。先看源代码。
public
class
HandlerTestActivity
extends
Activity {
/** Called when the activity is first created. */
private
ProgressBar pbar=
null
;
private
Button startBtn =
null
;
private
Button endBtn =
null
;
private
TextView timerView =
null
;
private
Button endtimer =
null
;
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
pbar = (ProgressBar)findViewById(R.id.pbar);
pbar.setMax(
100
);
startBtn = (Button)findViewById(R.id.startbtn);
endBtn = (Button)findViewById(R.id.endbtn);
timerView = (TextView)findViewById(R.id.timerView);
endtimer = (Button)findViewById(R.id.endtimer);
startBtn.setText(
"开始"
);
startBtn.setOnClickListener(
new
OnClickListener(){
public
void
onClick(View v) {
pbar.setVisibility(View.VISIBLE);
handler.post(runnable);
handler.post(timerRunnabler);
}
});
endBtn.setText(
"结束"
);
endBtn.setOnClickListener(
new
OnClickListener(){
public
void
onClick(View v) {
pbar.setProgress(
0
);
handler.removeCallbacks(runnable);
}
});
endtimer.setOnClickListener(
new
OnClickListener(){
public
void
onClick(View v) {
handler.removeCallbacks(timerRunnabler);
}
});
}
Handler handler =
new
Handler(){
@Override
public
void
handleMessage(Message msg){
int
intmsg = msg.what;
switch
(intmsg){
case
0
:
pbar.setProgress(msg.arg1);
if
(msg.arg1<pbar.getMax()){
handler.post(runnable);
}
else
{
handler.removeCallbacks(runnable);
}
break
;
case
1
:
timerView.setText(
"倒计时"
+msg.arg1);
handler.post(timerRunnabler);
break
;
}
}
};
Runnable runnable =
new
Runnable(){
int
i =
0
;
public
void
run(){
i= i+
5
;
Message msg=
new
Message();
msg.arg1=i;
msg.what=
0
;
Log.d(
"System.out"
,
"i="
+i+
"----runnableId="
+Thread.currentThread().getId()+
"|----runnableName="
+Thread.currentThread().getName());
try
{
Thread.sleep(
3000
);
}
catch
(InterruptedException e){
e.printStackTrace();
}
handler.sendMessage(msg);
if
(i==
80
){
pbar.setProgress(
0
);
handler.removeCallbacks(runnable);
//handler.removeMessages(0);
}
}
};
//计数器
Runnable timerRunnabler =
new
Runnable(){
int
j=
0
;
public
void
run() {
j=j+
1
;
Message msg =
new
Message();
msg.arg1=j;
msg.what=
1
;
Log.d(
"System.out"
,
"j="
+j+
"----timerRunnablerId="
+Thread.currentThread().getId()+
"|----timerRunnablerName="
+Thread.currentThread().getName());
try
{
Thread.sleep(
1000
);
}
catch
(InterruptedException e){
e.printStackTrace();
}
handler.sendMessage(msg);
}
};
}
操作界面是这样的我点击“开始”,运行runnable和timerRunnabler,,progressbar和倒计时开始进行。是设置runnable执行sleep,3秒后执行,timerRunnabler执行sleep,1秒后执行,但是从执行的效果上看是同步的,我的想法应该是不同步的。我在logcat中看到了,他们执行是按顺序来的,也就是进入了线程队列里了。两者应该是独立的,为什么会同步执行呢然后我点击“结束”,我让下面的倒计时任然继续。然后timerRunnabler执行是正常的,1秒一执行。于是我的问题:1、当点击"结束"按钮时,progressbar的progress没有归0,比如是50,点击"开始"时候,又在50的基础上开始输出,比如输出55
2、我这里是2个线程,runnable和timerRunnabler,为什么打印出当前线程的名字,是同一个,main
3、两个线程,runnable和timerRunnabler,执行的时间安排是,runnable:2000,timerRunnabler:1000,但实际执行的时候好像是同时执行的
progressbar增加一格,textview计数增加1,而点击"结束",progressbar不再执行时,timerRunnabler执行是1秒一执行。 请赐教啊handler的作用我理解是这样的,看图
版权所有©石家庄振强科技有限公司2024
冀ICP备08103738号-5
网站地图