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 service
onCreate
onStart
--- start service
onStart
--- stop service
onDestroy
--- bind service
onCreate
onBind
--- bind service
--- unBind service
onUnbind
onDestroy
--- start service
onCreate
onStart
--- bind service
onBind
--- stop service
--- unBind service
onUnbind
onDestroy
--- start service
onCreate
onStart
--- bind service
onBind
--- stop service
--- unBind service
onUnbind
onDestroy
前台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 service
mMyService = (MyService) iBinder;
// remote service
mMyServiceAidl = 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
参考:
Android Service完全解析,关于服务你所需知道的一切(上)
Android Service完全解析,关于服务你所需知道的一切(上)