最近在编写系统级service,借此也同时编写了java应用级service,感觉流程有点重要,编写还是没有太大问题。
很多情况下,一些与用户很少需要产生交互的应用程序,我们一般让它们在后台运行就行了,而且在它们运行期间我们仍然能运行其他的应用。
为了处理这种后台进程,Android引入了Service的概念。Service在Android中是一种长生命周期的组件,它不实现任何用户界面。最常见的例子如:媒体播放器程序,它可以在转到后台运行的时候仍然能保持播放歌曲;或者如文件下载程序,它可以在后台执行文件的下载。
让我们来看下如何创建Service:
创建一个Service
Android中已经定义了一个 ‘Service’类,所有其他的Service都继承于该类。Service类中定义了一系列的生命周期相关的方法,如: onCreate(), onStart(), onDestroy()。参见下例:
package com.andy.testApp.service;
public class MyService extends Service {
}
上例中的这个Service主要做了这些事:当服务创建和销毁时通过界面消息提示用户。
如Android中的其它部件一样, Service也会和一系列Intent关联。Service的运行入口需要在AndroidManifest.xml中进行配置,如下:
<service class=”.service.MyService”>
</service>
使用Service:
应用程序可以通过调用 Context.startService方法来启动Service。如果当前没有指定的Service实例被创建,则该方法会调用 Service的onCreate方法来创建一个实例;否则调用Service的onStart方法。参见下列代码:
..
Intent serviceIntent = new Intent();
serviceIntent.setAction(”com.andy.testApp.service.MY_SERVICE”);
startService(serviceIntent);
..
以上代码调用了startService方法,Service会持续运行,直到调用stopService()或stopSelf()方法。
还有另一种绑定Service的方式:
…
ServiceConnection conn = new ServiceConnection() {
};
bindService(new Intent(”com.andy.testApp.service.MY_SERVICE”), conn, Context.BIND_AUTO_CREATE);
…
当应用程序绑定一个Service后,该应用程序和Service之间就能进行互相通信,通常,这种通信的完成依靠于我们定义的一些接口,请看下例:
package com.andy.testApp;
public interface IMyService {
}
}
};
与远程Service通信(进程间Service通信):
如何两个进程间的Service需要进行通信,则需要把对象序列化后进行互相发送。
Android提供了一个 AIDL (Android接口定义语言)工具来处理序列化和通信。这种情况下Service需要以aidl文件的方式提供服务接口,AIDL工具将生成一个相应的java接口,并且在生成的服务接口中包含一个功能调用的stub服务桩类。Service的实现类需要去继承这个 stub服务桩类。Service的onBind方法会返回实现类的对象,之后你就可以使用它了,参见下例:
先创建一个IMyRemoteService.aidl文件,内容如下:
package com.andy.testApp;
interface IMyRemoteService {
}
如果你正在使用eclipse的 Android插件,则它会根据这个aidl文件生成一个Java接口类。生成的接口类中会有一个内部类Stub类,你要做的事就是去继承该Stub类:
package com.andy.testApp;
class RemoteService implements Service {
}
当客户端应用连接到这个Service时,onServiceConnected方法将被调用,客户端就可以获得IBinder对象。参看下面的客户端onServiceConnected方法:
…
ServiceConnection conn = new ServiceConnection() {
};
权限:
我们可以在AndroidManifest.xml文件中使用<service>标签来指定Service访问的权限:
<service class=”.service.MyService” android:permission=”com.andy.permission.MY_SERVICE_PERMISSION”>
</service>
之后应用程序要访问该Service的话就需要使用<user-permission>标签来指定相应的权限:
<uses-permission android:name=”com.andy.permission.MY_SERVICE_PERMISSION”>
</uses-permission>
总结如下:
1、编写一个应用层service:
首先service 继承自Service,通过Intent进行启动即可,使用时在AndroidManifest.xml进行声明及权限设定
2、系统层应用层service:
编写客户端manager接口给应用使用,通过binder机制(编写aidl文件自动生成客户端代码代码),然后在服务端实现aidl定义的接口,在systemServer中注册,使用时利用getSystemService获取服务调用即可。