Service

来源:互联网 发布:win8未激活网络怎么办 编辑:程序博客网 时间:2024/06/02 16:55

1. Service(服务)与Activiy都间接继承于Context。简单理解为是后台与前台的区别。

    Service也要在AndroidManifest.xml里面声明。

A Service is not a separate process. The Service object itself does not imply it is running in its own process; unless otherwise specified, it runs in the same process as the application it is part of.

A Service is not a thread. It is not a means itself to do work off of the main thread (to avoid Application Not Responding errors).

 

2.在Service中逻辑添加在onCreate和onStart中,如果做一些很耗时间的事情,最好在Service里启动一个线程来完成,因为Service是跑在主线程中,会影响到UI操作或者阻塞主线程中的其他事情.

 

3.两种启动方式(1).启动执行过程:context.startService(intent)->onCreate()->onStart()->服务运行,其中onCreate可以进行一些服务的初始化工作,onStart()则启动服务。
    停止执行过程下:context.stopService(intent)->onDestroy()->服务停止。
 http://blog.sina.com.cn/s/blog_5e32cc130100gvc7.html).启动执行过程:context.bindService(intent)->onCreate()->onBind()->服务运行(2停止执行过程下:context.unbindService()->onDestroy()->服务停止。

4. If we want to make this service run in a remote process (instead of the standard one for its .apk), we can use android:process in its manifest tag to specify one:

<service android:name=".app.MessengerService"        android:process=":remote" />

Note that the name "remote" chosen here is arbitrary, and you can use other names if you want additional processes. The ':' prefix appends the name to your package's standard process name.

原创粉丝点击