初学Handler有点疑惑

manymore13 2011-09-14 10:46:02
先上代码 说明一下问题


public class HandlerTextActivity extends Activity {
//声明按钮变量
Button startButton,endButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//获取按钮实例
startButton = (Button)findViewById(R.id.startButton);
endButton = (Button)findViewById(R.id.endButton);


//为按钮添加事件监听器
startButton.setOnClickListener(new startButtonClickListener());
endButton.setOnClickListener(new endButtonClickListener());

}
Thread thread =null;
Handler handler = new Handler();
class startButtonClickListener implements OnClickListener
{

@Override
public void onClick(View v)
{
// handler.post(updateThread);
thread = new Thread(updateThread);
thread.start();
}

}
class endButtonClickListener implements OnClickListener
{

@Override
public void onClick(View v)
{
flag = true;
}

}
boolean flag=false;
Runnable updateThread = new Runnable()
{

@Override
public void run()
{
System.out.println("updateThread");
while(true)
{
System.out.println("updateThread");
try
{
Thread.sleep(1000);
if(flag)
{
break;
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
//handler.postDelayed(updateThread, 1000);


}

};


}


用Handler和不用Handler都是每隔一秒输出updateThread 然后单击结束按钮就停止输出 。通过这个程序我只是觉得用Handler代码会少一点 没觉得有什么多大用处啊?用Thread都可以换他了,请大哥大姐指点一下。
...全文
191 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
傲慢的上校 2011-09-17
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 hedge31 的回复:]

为什么要用Hundler来更新UI,直接用重写Thread的run()的方法不也行吗?两者有什么不同?
[/Quote]只能在ui线程里面操作ui,不然会报错。
hedge31 2011-09-17
  • 打赏
  • 举报
回复
为什么要用Hundler来更新UI,直接用重写Thread的run()的方法不也行吗?两者有什么不同?
withtoday 2011-09-17
  • 打赏
  • 举报
回复
[Quote=引用楼主 manymore13 的回复:]
先上代码 说明一下问题

Java code



public class HandlerTextActivity extends Activity {
//声明按钮变量
Button startButton,endButton;
@Override
public void onCreate(Bundle savedInstanceState) ……
[/Quote]

你的注释看的我蛋疼....
OtherEyeOpen 2011-09-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 fishmen26 的回复:]
Handler 是android提供的一种异步操作的机制。 上面的代码中的handler 其实并不是运行在其他线程中的,而是在UI主线程中,通常实现异步操作都需要通过 handler + thread的方式。

上面代码中的thread运用是完全合理的,也达到了异步执行耗时操作的要求。但并不是android推荐的方式。至于android为什么要用handler 而不是thread 可以从 an……
[/Quote]
这个,明确解释什么是handler。。
简单的说,就是这两个单词的意思
一个是handler,一个是thread.
他们经常搭档,但明显不是一个玩意儿么
fishmen26 2011-09-15
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 manymore13 的回复:]

直接用handler就是在主线程中,用Thread.start()就开了个新线程???是不是这样子啊?
[/Quote]

是的哈,handler 是归属于你创建handler的那个线程。如果你在UI线程中创建,那它就属于UI线程,如果你在其他线程中创建,就属于其他线程。
manymore13 2011-09-14
  • 打赏
  • 举报
回复
直接用handler就是在主线程中,用Thread.start()就开了个新线程???是不是这样子啊?
  • 打赏
  • 举报
回复
如果你用到Toast等影响界面显示的操作的时候,就能发现Thread和Handler的区别,一边用Handler或者ShowOnUiThread等方式显示Toast等信息
stonebreakers 2011-09-14
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 kechanghe0705 的回复:]

Handler 是一个连接线程(thread)与UI的一个桥梁,比如在Thread里面操作数据 要即时更新,你就要使用Handler 了。但是LZ也许会问,操作复杂的东西放在Handler不就可以了吗,我告诉你是不可以的,如果操作复杂而又耗时的操作放在Handler是会报ANR错误的,因为Handler是与系统UI在同一个线程里,因为UI不允许操作耗时的操作,所以handler也不允许有耗时的操……
[/Quote]这个解释很正确,明白人!!赞一个
zyking1987 2011-09-14
  • 打赏
  • 举报
回复
如果有信息 需要push 你这thread里怎么实现呢??
ch_984326013 2011-09-14
  • 打赏
  • 举报
回复
这个是效率问题!明白否?
manymore13 2011-09-14
  • 打赏
  • 举报
回复
上面那段E文石api里的吧 没怎么看懂....
j_f0001 2011-09-14
  • 打赏
  • 举报
回复
Handler 是一个连接线程(thread)与UI的一个桥梁,比如在Thread里面操作数据 要即时更新,你就要使用Handler 了。但是LZ也许会问,操作复杂的东西放在Handler不就可以了吗,我告诉你是不可以的,如果操作复杂而又耗时的操作放在Handler是会报ANR错误的,因为Handler是与系统UI在同一个线程里,因为UI不允许操作耗时的操作,所以handler也不允许有耗时的操作,否则就有ANR错误
fishmen26 2011-09-14
  • 打赏
  • 举报
回复
Handler 是android提供的一种异步操作的机制。 上面的代码中的handler 其实并不是运行在其他线程中的,而是在UI主线程中,通常实现异步操作都需要通过 handler + thread的方式。

上面代码中的thread运用是完全合理的,也达到了异步执行耗时操作的要求。但并不是android推荐的方式。至于android为什么要用handler 而不是thread 可以从 android 对 handler的解释中找到一些线索。

A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue. Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.

There are two main uses for a Handler: (1) to schedule messages and runnables to be executed as some point in the future; and (2) to enqueue an action to be performed on a different thread than your own.

Scheduling messages is accomplished with the post(Runnable), postAtTime(Runnable, long), postDelayed(Runnable, long), sendEmptyMessage(int), sendMessage(Message), sendMessageAtTime(Message, long), and sendMessageDelayed(Message, long) methods. The post versions allow you to enqueue Runnable objects to be called by the message queue when they are received; the sendMessage versions allow you to enqueue a Message object containing a bundle of data that will be processed by the Handler's handleMessage(Message) method (requiring that you implement a subclass of Handler).

When posting or sending to a Handler, you can either allow the item to be processed as soon as the message queue is ready to do so, or specify a delay before it gets processed or absolute time for it to be processed. The latter two allow you to implement timeouts, ticks, and other timing-based behavior.

When a process is created for your application, its main thread is dedicated to running a message queue that takes care of managing the top-level application objects (activities, broadcast receivers, etc) and any windows they create. You can create your own threads, and communicate back with the main application thread through a Handler. This is done by calling the same post or sendMessage methods as before, but from your new thread. The given Runnable or Message will then be scheduled in the Handler's message queue and processed when appropriate.

注意文中提到 When a process is created for your application, its main thread is dedicated to running a message queue that takes care of managing the top-level application objects (activities, broadcast receivers, etc) and any windows they create.

所以android的application实现方式就是通过 message queue来管理消息的,所以handler是android中最适合管理异步操作的方式。

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧