Android,UI主线程与子线程

在一个Android 程序开始运行的时候,会单独启动一个Process。默认的情况下,所有这个程序中的Activity或者Service(Service和 Activity只是Android提供的Components中的两种,除此之外还有Content Provider和Broadcast Receiver)都会跑在这个Process的主线程上,所以在默认设置下,service需要开线程来处理I/O等操作。
摘至API guides:
Caution: A service runs in the main thread of its hosting process—the service does not create its own thread and does not run in a separate process (unless you specify otherwise). This means that, if your service is going to do any CPU intensive work or blocking operations (such as MP3 playback or networking), you should create a new thread within the service to do that work. By using a separate thread, you will reduce the risk of Application Not Responding (ANR) errors and the application's main thread can remain dedicated to user interaction with your activities.


设置<service android:name=".controler.service.PlayerService" android:process=":remote"/>
对服务使用另一个进程,这样在服务里面进行耗时的操作。主线程还是不影响的

        一个Android 程序默认情况下也只有一个Process,但一个Process下却可以有许多个Thread。
  
       在这么多Thread当中,有一个Thread,我们称之为UI Thread。UI Thread在Android程序运行的时候就被创建,是一个Process当中的主线程Main Thread,主要是负责控制UI界面的显示、更新和控件交互。在Android程序创建之初,一个Process呈现的是单线程模型,所有的任务都在一 个线程中运行。因此,我们认为,UI Thread所执行的每一个函数,所花费的时间都应该是越短越好。而其他比较费时的工作(访问网络,下载数据,查询数据库等),都应该交由子线程去执行, 以免阻塞主线程。


那么,UI Thread如何和其他Thread一起工作呢?常用方法是:
        诞生一个主线程的Handler物件,当做Listener去让子线程能将讯息Push到主线程的Message Quene里,以便触发主线程的handlerMessage()函数,让主线程知道子线程的状态,并在主线程更新UI。

       例如,在子线程的状态发生变化时,我们需要更新UI。如果在子线程中直接更新UI,通常会抛出下面的异常:
  
        11-07 13:33:04.393: ERROR/JavaBinder(1029):android.view.ViewRoot$CalledFromWrongThreadException:Only the original thread that created a view hierarchy can touch its views.


service有两种启动方式,一种是直接startservice方式启动,这样启动的service是一个单独运行的,并不会随着activity的关闭而关闭,也就是说这种方式启动的service的生命周期和activity的是没有关联的。还有一种是通过binder的方式吧,这样启动的service的生命周期和activity是相同的,也就是说当activity关闭的时候此service也停止了

猜你喜欢

转载自dengzhangtao.iteye.com/blog/1831692
今日推荐