博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Service详解
阅读量:5835 次
发布时间:2019-06-18

本文共 5501 字,大约阅读时间需要 18 分钟。

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

复制代码

参考:

转载地址:http://gwycx.baihongyu.com/

你可能感兴趣的文章
高雄购房调查:本地与外地人都看好三块区域
查看>>
中国环境损害司法鉴定机构突破100家
查看>>
首创置业“中国之家-和园”将在巴黎开工奠基
查看>>
NoSQL还是SQL?这一篇讲清楚
查看>>
建议收藏:你想要的开发和设计资源全部都在这里
查看>>
基于 D3.js 绘制动态进度条
查看>>
你为什么会被解雇? — Top 10 面试问题解析
查看>>
lodash源码分析之缓存使用方式的进一步封装
查看>>
Android复杂数据流的“高效”渲染(二)
查看>>
Android监测用户行为之中AOP编程之AspectJ实战
查看>>
用 Swift 来刷 leet code 吧 (1-20)
查看>>
20分钟上手 webAssembly
查看>>
SpringBoot下使用定时任务的方式全揭秘
查看>>
强引用、弱引用、软引用
查看>>
Maven构建自动化
查看>>
刷前端面经笔记(四)
查看>>
你真的会阅读Java的异常信息吗?
查看>>
完善的IaaS云服务的个人理解
查看>>
基于Kerberos的大数据安全方案
查看>>
IO流异常:Stream closed
查看>>