关于Service

1.     一个Service是一段长生命周期的,没有用户界面的后台服务。Service的启动和停止由Activity或者其它Context对象控制的。

2.     如果你在ServiceonCreate或者onStart做一些很耗时间的事情,最好在Service里启动一个线程来完成,因为Service是运行在主线程中,会影响到你的UI操作或者阻塞主线程中的其他事情。

3.     Android系统将会尝试保留那些启动了的或者是绑定了服务的进程。如果该服务正在进程的onCreate(), onStart(), 或者 onDestroy() 这些方法中执行时那么主进程将会成为一个前台进程,以确保此代码不会被停止。如果服务已经开始,那么它的主进程会就重要性而言低于所有可见的进程但高于不可见的进程由于只有少数几个进程是用户可见的,所以只要不是内存特别低,该服务不会停止.。如果有多个客户端绑定了服务只要客户端中的一个对于用户是可见的,即认为该服务可见。

4.     service配置

<service android:enabled=["true" | "false"]

         android:exported[="true" | "false"]

         android:icon="drawable resource"

         android:label="string resource"

         android:name="string"

         android:permission="string"

         android:process="string" >

</service>

contained in:

<application>

can contain:

<intent-filter> 
<meta-data>

android:exported

Whether or not components of other applications can invoke the service or interact with it — "true" if they can, and "false" if not. When the value is "false", only components of the same application or applications with the same user ID can start the service or bind to it.

是否允许其它应用启动此service “true”可以,“false”不可以,只允许本应用或有相同UserID的应用使用.

Default值取决于此Service是否包含IntentFilters.

<manifest

    xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.android.browser"

    android:sharedUserId="@string/sharedUserId"

    >

android:permission

如果一个Service 申明了一个android:permission ,  发启者没有被授予这个permission,此方法将不被调用,并且此Service无法接收到发出的Intent.

如果此属性没有设置,那么permission的值将与<application> <permission>属性的值相同,如果都没有设置那么此service 不被permission保护.

The name of a permission that that an entity must have in order to launch the service or bind to it. If a caller of startService(), bindService(), or stopService(), has not been granted this permission, the method will not work and the Intent object will not be delivered to the service.

If this attribute is not set, the permission set by

 the <application> element's permission attribute applies to the service. If neither attribute is set, the service is not protected by a permission.

android:process

The name of the process where the service is to run. Normally, all components of an application run in the default process created for the application. It has the same name as the application package.

The <application> element's process attribute can set a different default for all components. But component can override the default with its ownprocess attribute, allowing you to spread your application across multiple processes.

If the name assigned to this attribute begins with a colon (':'), a new process, private to the application, is created when it's needed and the service runs in that process. If the process name begins with a lowercase character, the service will run in a global process of that name, provided that it has permission to do so. This allows components in different applications to share a process, reducing resource usage.

service将要运行在此进程中,一般情况下,一个应用的所有组件运行在为应用建立的默认进程中,此进程名与application package 名称是一样的,

<application> 属性<process> 可以为所有组件设置不同的默认值,但是组件可以通过重写自己的process属性值,允许去提高应用速度通过多进程。

如果属性值以”:”开始,那此应用的一个私有进程将被建立,如果需要,并且service在此进程中,如果此进程名以小写字符开始,此Service将运行在以此名字的一个全局的进程中。并给予权限,这样就充许在不同应用中的组件共享同一进程,减少资源利用。

5.     service启动

1)   通过调用Context.startService()启动,调用Context.stopService()结束,startService()可以传递参数给Service

2)     过调用Context.bindService()启动,调用Context.unbindservice()结束,还可以通过ServiceConnection访问Service

6.     service生命周期

1)     startService后,如果Service还没有运行,则android先调用onCreate()然后调用onStart();如果Service已经运行,则只调用onStart(),所以一个ServiceonStart方法可能会重复调用多次。即使调用startService的进程结束了,Service仍然还存在,直到有进程调用stopService,或者Service自己自杀(stopSelf())。stopService的时候直接onDestroy,如果是调用者自己直接退出而没有调用stopService的话,Service会一直在后台运行。该Service的调用者再启动起来后可以通过stopService关闭Service

onCreate --> onStart(可多次调用) --> onDestroy

2)     bindService后,Service就和调用bindService的进程同生共死了,也就是说当调用bindService的进程死了,那么它bindService也要跟着被结束,当然期间也可以调用unbindservice让 Service结束。

context.bindService()->onCreate()->onBind()->Service running

onUnbind() -> onDestroy() ->Service stop

onCreate --> onBind(只一次,不可多次绑定) --> onUnbind --> onDestory

onBind将返回给客户端一个IBind接口实例,IBind允许客户端回调服务的方法,比如得到Service运行的状态或其他操作。这个时候把调用者(Context,例如Activity)会和Service绑定在一起,Context退出了,Srevice就会调用onUnbind->onDestroy相应退出。

注:两种方式混合使用时,比如说你startService了,我bindService了,那么只有你stopService了而且也unbindservice了,这个Service才会被结束。

所以,在Service每一次的开启关闭过程中,只有onStart可被多次调用(通过多次startService调用),其他onCreateonBindonUnbindonDestory在一个生命周期中只能被调用一次。

 

7.     两个误区

1>Service 并不是运行在独立的进程中,Service与应用本身运行在同一进程中。

2>Service 不是一个线程,Service并没有脱离main thread而工作

为了避免ANR errorsService 提供了两个主要的特征:

1> 便于应用通知系统一些动作需要在后台动行(不需要用户驱动).

      通过Context.startService() :通行之前要确保stop it.

2> A facility for an application to expose(受到,揭发) some of its functionality(功能性) to other applications.

Context.bindService():允许长时间存在的service,以便与其它应用相互通信。

8.     manifest中被声明的Service,可以被全局执行。如果其它应用想要调用它,需在自己的manifest中定义对应的<uses-permission>

9.     远程service调用:aidl

Stub Class:存根类,调用该类的asInterface方法,转换Ibinder对象为对应接口,生成代理。

Proxy Class:静态内部代理类,实现对应接口。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值