Android service几个问题

Android service几个问题

记录service二三事

问题1

在一些系统中使用**startService可以启动服务,bindService无法启动服务**

进程A bindService 服务端的进程B。
进程B没启动,进程A bindService无效
进程B已启动,进程A bindService 成功

原因:
android7 新增了一个 关联启动(StartUp)权限,默认没有开启,所以Service进程B就没办法 被进程A启动。

解决:
打开设置----》应用列表—》选择服务端的应用—》权限----》开启 关联启动

问题2

同一个应用多次绑定Service,onServiceConnect会执行几次?Service的函数会执行哪些?

  • 同一个ServiceConnection 不会回调多次
  • 只有第一次bindService会执行Service的onCreate方法
  • 同一个进程bind服务,Service只执行一次onBind方法
  • 同一个ServiceConnection只会回调一次onServiceConnet,不同ServiceConnection会回调一次onServiceConnect
07-12 17:38:06.443 14805 14805 D ServiceBinderStatusTest: onClick: bind_service 
07-12 17:38:06.560 14981 14981 D ServiceBinderStatusTest: onCreate: 
07-12 17:38:06.561 14981 14981 D ServiceBinderStatusTest: onBind: 1
07-12 17:38:06.561 14805 14805 D ServiceBinderStatusTest: onServiceConnected: 
07-12 17:38:08.312 14805 14805 D ServiceBinderStatusTest: onClick: bind_service2
07-12 17:38:08.313 14805 14805 D ServiceBinderStatusTest: onClick: bindService true
07-12 17:38:08.313 14805 14805 D ServiceBinderStatusTest: onServiceConnected 2: 
07-12 17:38:14.069 14805 14805 D ServiceBinderStatusTest: onClick: bind_service 
07-12 17:38:15.235 14805 14805 D ServiceBinderStatusTest: onClick: bind_service 
07-12 17:38:17.200 14805 14805 D ServiceBinderStatusTest: onClick: bind_service2
07-12 17:38:17.203 14805 14805 D ServiceBinderStatusTest: onClick: bindService true
07-12 17:38:17.203 14805 14805 D ServiceBinderStatusTest: onServiceConnected 2: 
07-12 17:38:26.685 14805 14805 D ServiceBinderStatusTest: onClick: bind_service2
07-12 17:38:26.689 14805 14805 D ServiceBinderStatusTest: onClick: bindService true
07-12 17:38:26.694 14805 14805 D ServiceBinderStatusTest: onServiceConnected 2: 
07-12 17:38:28.203 14805 14805 D ServiceBinderStatusTest: onClick: bind_service 
07-12 17:38:33.446 14805 14805 D ServiceBinderStatusTest: onClick: bind_service2
07-12 17:38:33.450 14805 14805 D ServiceBinderStatusTest: onClick: bindService true
07-12 17:38:33.452 14805 14805 D ServiceBinderStatusTest: onServiceConnected 2:

问题3

生命周期

  • startService -> stopService

    07-12 17:56:27.928 26644 26644 D ServiceBinderStatusTest: onClick: start_service
    07-12 17:56:27.935 14981 14981 D ServiceBinderStatusTest: onCreate: 
    07-12 17:56:27.946 14981 14981 D ServiceBinderStatusTest: onStartCommand: 
    07-12 17:56:27.946 14981 14981 D ServiceBinderStatusTest: onStart: 
    
    07-12 17:56:37.871 26644 26644 D ServiceBinderStatusTest: onClick: stop_service
    07-12 17:56:37.877 14981 14981 D ServiceBinderStatusTest: onDestroy
    
  • bindService -> unbindService

    07-12 18:08:43.871  2672  2672 D ServiceBinderStatusTest: onClick: bind_service 
    07-12 18:08:43.877 14981 14981 D ServiceBinderStatusTest: onCreate: 
    07-12 18:08:43.878 14981 14981 D ServiceBinderStatusTest: onBind: 1
    07-12 18:08:43.879  2672  2672 D ServiceBinderStatusTest: onServiceConnected: 
    07-12 18:08:45.528  2672  2672 D ServiceBinderStatusTest: onClick: unbindService
    07-12 18:08:45.531 14981 14981 D ServiceBinderStatusTest: onUnbind: 1
    07-12 18:08:45.531 14981 14981 D ServiceBinderStatusTest: onDestroy: 
    
    
  • bindService -> startService -> unbindService -> stopService

    07-12 18:09:56.550  3414  3414 D ServiceBinderStatusTest: onClick: bind_service 
    07-12 18:09:56.554 14981 14981 D ServiceBinderStatusTest: onCreate: 
    07-12 18:09:56.554 14981 14981 D ServiceBinderStatusTest: onBind: 1
    07-12 18:09:56.559  3414  3414 D ServiceBinderStatusTest: onServiceConnected: 
    07-12 18:09:59.734  3414  3414 D ServiceBinderStatusTest: onClick: start_service
    07-12 18:09:59.740 14981 14981 D ServiceBinderStatusTest: onStartCommand: 
    07-12 18:09:59.740 14981 14981 D ServiceBinderStatusTest: onStart: 
    07-12 18:10:06.191  3414  3414 D ServiceBinderStatusTest: onClick: unbindService
    07-12 18:10:06.199 14981 14981 D ServiceBinderStatusTest: onUnbind: 1
    07-12 18:10:08.281  3414  3414 D ServiceBinderStatusTest: onClick: stop_service
    07-12 18:10:08.287 14981 14981 D ServiceBinderStatusTest: onDestroy: 
    

    unbindService 和 stopService都执行之后才会调用onDestroy

  • startService -> bindService -> unbindService -> stopService

    07-12 18:11:12.005  3414  3414 D ServiceBinderStatusTest: onClick: start_service
    07-12 18:11:12.010 14981 14981 D ServiceBinderStatusTest: onCreate: 
    07-12 18:11:12.010 14981 14981 D ServiceBinderStatusTest: onStartCommand: 
    07-12 18:11:12.010 14981 14981 D ServiceBinderStatusTest: onStart: 
    07-12 18:11:13.606  3414  3414 D ServiceBinderStatusTest: onClick: bind_service 
    07-12 18:11:13.613 14981 14981 D ServiceBinderStatusTest: onBind: 1
    07-12 18:11:13.618  3414  3414 D ServiceBinderStatusTest: onServiceConnected: 
    07-12 18:11:16.191  3414  3414 D ServiceBinderStatusTest: onClick: unbindService
    07-12 18:11:16.198 14981 14981 D ServiceBinderStatusTest: onUnbind: 1
    07-12 18:11:19.122  3414  3414 D ServiceBinderStatusTest: onClick: stop_service
    07-12 18:11:19.135 14981 14981 D ServiceBinderStatusTest: onDestroy: 
    
  • bindService -> unbindService -> stopService

    07-12 18:12:14.835  3414  3414 D ServiceBinderStatusTest: onClick: bind_service 
    07-12 18:12:14.853 14981 14981 D ServiceBinderStatusTest: onCreate: 
    07-12 18:12:14.859 14981 14981 D ServiceBinderStatusTest: onBind: 1
    07-12 18:12:14.864  3414  3414 D ServiceBinderStatusTest: onServiceConnected: 
    07-12 18:12:15.792  3414  3414 D ServiceBinderStatusTest: onClick: stop_service
    07-12 18:12:18.114  3414  3414 D ServiceBinderStatusTest: onClick: unbindService
    07-12 18:12:18.119 14981 14981 D ServiceBinderStatusTest: onUnbind: 1
    07-12 18:12:18.120 14981 14981 D ServiceBinderStatusTest: onDestroy: 
    
  • startService -> unbindService -> stopService

    07-12 18:13:25.745  5715  5715 D ServiceBinderStatusTest: onClick: start_service
    07-12 18:13:25.752 14981 14981 D ServiceBinderStatusTest: onStartCommand: 
    07-12 18:13:25.752 14981 14981 D ServiceBinderStatusTest: onStart: 
    07-12 18:13:26.822  5715  5715 D ServiceBinderStatusTest: onClick: unbindService
    07-12 18:13:26.822  5715  5715 D AndroidRuntime: Shutting down VM
    07-12 18:13:26.822  5715  5715 E AndroidRuntime: FATAL EXCEPTION: main
    07-12 18:13:26.822  5715  5715 E AndroidRuntime: Process: jun.servicebinderstatustest, PID: 5715
    07-12 18:13:26.822  5715  5715 E AndroidRuntime: java.lang.IllegalArgumentException: Service not registered: jun.servicebinderstatustest.MainActivity$1@722cdb7
    07-12 18:13:26.822  5715  5715 E AndroidRuntime: 	at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:1757)
    07-12 18:13:26.822  5715  5715 E AndroidRuntime: 	at android.app.ContextImpl.unbindService(ContextImpl.java:1874)
    07-12 18:13:26.822  5715  5715 E AndroidRuntime: 	at android.content.ContextWrapper.unbindService(ContextWrapper.java:792)
    07-12 18:13:26.822  5715  5715 E AndroidRuntime: 	at android.content.ContextWrapper.unbindService(ContextWrapper.java:792)
    07-12 18:13:26.822  5715  5715 E AndroidRuntime: 	at jun.servicebinderstatustest.MainActivity.onClick(MainActivity.java:120)
    07-12 18:13:26.822  5715  5715 E AndroidRuntime: 	at android.view.View.performClick(View.java:7448)
    07-12 18:13:26.822  5715  5715 E AndroidRuntime: 	at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1194)
    07-12 18:13:26.822  5715  5715 E AndroidRuntime: 	at android.view.View.performClickInternal(View.java:7425)
    07-12 18:13:26.822  5715  5715 E AndroidRuntime: 	at android.view.View.access$3600(View.java:810)
    07-12 18:13:26.822  5715  5715 E AndroidRuntime: 	at android.view.View$PerformClick.run(View.java:28305)
    07-12 18:13:26.822  5715  5715 E AndroidRuntime: 	at android.os.Handler.handleCallback(Handler.java:938)
    07-12 18:13:26.822  5715  5715 E AndroidRuntime: 	at android.os.Handler.dispatchMessage(Handler.java:99)
    07-12 18:13:26.822  5715  5715 E AndroidRuntime: 	at android.os.Looper.loop(Looper.java:223)
    07-12 18:13:26.822  5715  5715 E AndroidRuntime: 	at android.app.ActivityThread.main(ActivityThread.java:7656)
    07-12 18:13:26.822  5715  5715 E AndroidRuntime: 	at java.lang.reflect.Method.invoke(Native Method)
    07-12 18:13:26.822  5715  5715 E AndroidRuntime: 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    07-12 18:13:26.822  5715  5715 E AndroidRuntime: 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
    07-12 18:13:26.837  5715  5715 I Process : Sending signal. PID: 5715 SIG: 9
    07-12 18:13:26.854   271   271 I Zygote  : Process 5715 exited due to signal 9 (Killed)
    
    

问题4

Exception :“Service not registered”

在调用unbindService时,如果connection没有注册或者已经被unbind了,系统会抛出异常java.lang.IllegalArgumentException: Service not registered

问题5

bindService返回true,Service也正常启动,但是没有onServiceConnect回调事件

onBind返回结果为null时没有回调。

OnBind方法中需返回一个IBinder实例,不然onServiceConnected方法不会调用。

问题6

生命周期中startService 不会执行onBind,bindService 不会执行 onStartCommandonStart

  • startService

    07-12 18:18:19.884  8816  8816 D ServiceBinderStatusTest: onClick: start_service
    07-12 18:18:19.889 14981 14981 D ServiceBinderStatusTest: onCreate: 
    07-12 18:18:19.890 14981 14981 D ServiceBinderStatusTest: onStartCommand: 
    07-12 18:18:19.890 14981 14981 D ServiceBinderStatusTest: onStart: 
    
  • bindService

    07-12 18:19:33.882  9760  9760 D ServiceBinderStatusTest: onClick: bind_service 
    07-12 18:19:33.999  9903  9903 D ServiceBinderStatusTest: onCreate: 
    07-12 18:19:33.999  9903  9903 D ServiceBinderStatusTest: onBind: 1
    07-12 18:19:34.000  9760  9760 D ServiceBinderStatusTest: onServiceConnected: 
    

问题7

bindService的返回值

  • bindService()的返回值只是表明服务是否存在;

  • 真正能代表是否成功绑定服务的是触发onServiceConnected回调。

bindService为异步操作,执行完bindService,并 不会马上回调onServiceConnected,当然也不会等待它;而是按照程序的顺序继续执行。这时由于服务还未成功获取到,调用服务中的方法就会出错。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值