Local Service
Start and stop
Manifest
复制代码
val intent = Intent(this, MyService::class.java)startService(intent)stopService(intent)复制代码
Bind and unbind
MyService
class MyService : Service() { class LocalBinder : Binder() { fun getTime(): Long { return System.currentTimeMillis() } } private var mLocalBinder = LocalBinder() override fun onBind(intent: Intent): IBinder? { log("onBind") return mLocalBinder }}复制代码
private val mServiceConnection = object : ServiceConnection { override fun onServiceConnected(name: ComponentName, service: IBinder) { mLocalService = service as MyService.LocalBinder } override fun onServiceDisconnected(name: ComponentName) { }}val intent = Intent(this, MyService::class.java)bindService(intent)unbindService(intent)复制代码
start,stop,bind,unbind 混合调用
- stopService只会让Service停止,unbindService只会让Service和context解除关联。
- 一个service必须要在既没有和任何context关联又处在停止状态的时候才会被销毁。
start,stop,bind,unbind 各类调用场景日志
--- start serviceonCreateonStart--- start serviceonStart--- stop serviceonDestroy复制代码
--- bind serviceonCreateonBind--- bind service--- unBind serviceonUnbindonDestroy复制代码
--- start serviceonCreateonStart--- bind serviceonBind--- stop service--- unBind serviceonUnbindonDestroy复制代码
--- start serviceonCreateonStart--- bind serviceonBind--- stop service--- unBind serviceonUnbindonDestroy复制代码
前台Service
调用service的startForeground即可
@Override public void onCreate() { super.onCreate(); startForeground(1, new Notification(R.mipmap.ic_launcher, "title", System.currentTimeMillis())); }复制代码
Remote Service
Removete service 在另一个进程中运行。不能直接bind,要bind要AIDL
RemoteService 声明
复制代码
外部APP启动remote service
Intent intent = new Intent("com.example.servicetest.MyAIDLService");bindService(intent, connection, BIND_AUTO_CREATE);复制代码
AIDL 使用步骤
1. 在项目中新建aild文件(例如 IMServiceAidl.aidl),在文件中写接口函数,sync工程,IDE会自动生成aidl对应的文件(IMServiceAidl.java),文件中有。
编写的 IMyServiceAidl.aidl
package com.example.demo0109.service;interface IMyServiceAidl { long getTime();}复制代码生成类 IMyServiceAidl.java 的结构
2. bind service
private ServiceConnection mServiceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { mMyServiceAidl = IMyServiceAidl.Stub.asInterface(service); } @Override public void onServiceDisconnected(ComponentName name) { mMyServiceAidl = null; } };复制代码
bind service的时候,remote service 与 local service 的区别
// common servicemMyService = (MyService) iBinder;// remote servicemMyServiceAidl = IMyServiceAidl.Stub.asInterface(iBinder);复制代码
附录:完整代码
MainActivity.kt
class MainActivity : AppCompatActivity() { private var mRemoteBinder: IMyServiceAidl? = null private var mLocalService: MyService.LocalBinder? = null private val mServiceConnection = object : ServiceConnection { override fun onServiceConnected(name: ComponentName, service: IBinder) { mLocalService = service as MyService.LocalBinder// mRemoteBinder = IMyServiceAidl.Stub.asInterface(service) } override fun onServiceDisconnected(name: ComponentName) { } } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val intent = Intent(this, MyService::class.java) bt_start_service.setOnClickListener { log("--- start service") startService(intent) } bt_stop_service.setOnClickListener { log("--- stop service") stopService(intent) } bt_bind_service.setOnClickListener { log("--- bind service") bindService(intent, mServiceConnection, Service.BIND_AUTO_CREATE) } bt_unbind_service.setOnClickListener { log("--- unBind service") unbindService(mServiceConnection) } } private fun log(s: String) { Log.d("MyService", s) }}复制代码
MyService.kt
class MyService : Service() { class LocalBinder : Binder() { fun getTime(): Long { return System.currentTimeMillis() } } private var mLocalBinder = LocalBinder() private var mRemoteBinder: IMyServiceAidl.Stub = object : IMyServiceAidl.Stub() { @Throws(RemoteException::class) override fun getTime(): Long { return System.currentTimeMillis() } } override fun onBind(intent: Intent): IBinder? { log("onBind") return mLocalBinder// return mRemoteBinder } override fun onUnbind(intent: Intent): Boolean { log("onUnbind") return super.onUnbind(intent) } override fun onCreate() { super.onCreate() startForeground(1, Notification(R.mipmap.ic_launcher, "title", System.currentTimeMillis())) log("onCreate") } override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { log("onStart") return super.onStartCommand(intent, flags, startId) } override fun onDestroy() { super.onDestroy() log("onDestroy") } private fun log(s: String) { Log.d(javaClass.simpleName, s) }}复制代码
IMyServiceAidl.aidl
package com.example.demo0109.service;interface IMyServiceAidl { long getTime();}复制代码
Manifest.xml
复制代码
参考: