zoukankan      html  css  js  c++  java
  • AIDLservice

    有三种情况:如果直接使用服务,则没有必要进行绑定,但是如果要使用服务里面的方法,则要进行绑定。具体的启动情况有下:

    其中很重要一点:bindService和unbindService是成对出现的。

     1.当启动时,单独调用bindService方法,在unbindService后,会执行service的onUnbind。之后会调用onDestory

    a. 首次使用bindService方法会调用service端的onCreate()->onBinder(),此时如果调用service端的方法是OK的。

    1 I/AIDLService( 5832): service onCreate()...
    2 I/AIDLService( 5832): ...service onBind()...
    3 I/AIDLService( 5809): onServiceConnected....

    b. 再次使用bindservice方法则不会重复调用,即无调用。此时如何调用service端的方法是OK的。

    c. 在解绑定时候调用unbindService方法时候(或者绑定服务的activity destory后)会调用service端的onUnbind()会调用onDestory()方法。此时服务虽然销毁了,但是仍然可以调用service端的方法

    1 I/AIDLService( 5809): unbindService
    2 I/AIDLService( 5809): unbindService
    3 I/AIDLService( 5832): service onUnbind()...
    4 I/AIDLService( 5832): service onDestroy()...

     2.当启动时,先调用startService,再调用bindService方法后,在unbindService后,会执行service的onUnbind,不会执行onDestroy方法。除非你在执行stopService.

    a. 先调用startService,再调用bindService方法后会执行此调用:onCreate()->onBinder()->onStartCommand() 此时可以调用service端的方法。

    startService
    I/AIDLService( 5832): service onCreate()... I/AIDLService( 5832): service onStartCommand...

    bindService
    1
    I/AIDLService( 5832): ...service onBind()... 2 I/AIDLService( 5809): onServiceConnected....

    b. 再次使用bindservice方法则不会重复调用,即无调用。

    c. 在unbindService后,会调用onUnbind(),此时服务解绑定了,但是仍然可以调用service端的方法。

    1 I/AIDLService( 5809): unbindService
    2 I/AIDLService( 5832): service onUnbind()...

     3. 先调用startService,在调用stopService,会执行service的onDestroy方法。

    a. 先调用startService,再调用bindService方法后会执行此调用:onCreate()->onBinder()->onStartCommand() 此时可以调用service端的方法。

    I/AIDLService( 1909): btn=2131230720
    I/AIDLService( 2092): service onCreate()...
    I/AIDLService( 2092): service onStartCommand...
    I/AIDLService( 1909): btn=2131230721
    I/AIDLService( 1909): btn_bindService
    I/AIDLService( 2092): ...service onBind()...
    I/AIDLService( 1909): onServiceConnected....

    b. 再次使用bindservice方法则不会重复调用。

    c. 调用stopService后,仍然可以再调用service端的方法。(但是如何调用了bindService后没有调用unbindService而直接stopservice,那么会导致service没有正常的destory)

    I/AIDLService( 2140): btn=2131230724
    I/AIDLService( 2140): unbindService
    I/AIDLService( 2140): unbindService
    I/AIDLService( 2092): service onUnbind()...
    
    I/AIDLService( 2140): btn=2131230725
    I/AIDLService( 2140): btn_stopService
    I/AIDLService( 2140): btn_stopService
    I/AIDLService( 2092): service onDestroy()...
  • 相关阅读:
    第十五周翻译
    数据库 第十五周学习笔记
    第十四周学习笔记
    SQL Server安全级别2的楼梯:身份验证
    第十三周学习笔记
    第十三周翻译:SQL Server的安全1级楼梯:SQL Server安全概述
    MySQL修改默认存储引擎(转)
    【整理】MySQL引擎(转)
    合理配置MySQL缓存 提高缓存命中率(转)
    MySQL数据库分区的概念与2大好处
  • 原文地址:https://www.cnblogs.com/kernel-style/p/4766752.html
Copyright © 2011-2022 走看看