bindService后,onServiceConnected方法没有执行
2017-05-26 16:16
423 查看
在体验bindService功能时候,执行过bindService后,也返回绑定成功,但是发现获取Service的对象mBoundService一直为空,onServiceConnected没有执行,一个晚上没想明白,也尝试了网上的一些做法,直到看到了下面的方法(http://www.itwendao.com/article/detail/286559.html),搞定。
即下面mBoundService引用的代码从onCreate挪到onServiceConnected里去
官网对于bindService有句描述:
Connect to an application service, creating it if needed. This defines a dependency between your application and the service. The given conn will receive the service object when it is created and be told if it dies and restarts. The service will be considered required by the system only for as long as the calling context exists. For example, if this Context is an Activity that is stopped, the service will not be required to continue running until the Activity is resumed.
大意是,onServiceConnected在绑定成功时进行回调,但不保证在执行bindService后立马回调,我们在onCreate方法中绑定后立马获取service实例,但此时不保证onServiceConnected已经被回调。 也就是我们onCreate方法执行时onServiceConnected还没有别调用。此时当然mBoundService还为空了。
即下面mBoundService引用的代码从onCreate挪到onServiceConnected里去
mBoundService = (mMusicBinder).getService(); mBoundService.addMusicStateChangedListener(MainActivity.this); mBoundService.setPath(DatabaseModel.getDatabaseModelInstance(MainActivity.this) .getMusicItemById(1).getPath()); mBoundService.setPlayingId(1);
官网对于bindService有句描述:
Connect to an application service, creating it if needed. This defines a dependency between your application and the service. The given conn will receive the service object when it is created and be told if it dies and restarts. The service will be considered required by the system only for as long as the calling context exists. For example, if this Context is an Activity that is stopped, the service will not be required to continue running until the Activity is resumed.
大意是,onServiceConnected在绑定成功时进行回调,但不保证在执行bindService后立马回调,我们在onCreate方法中绑定后立马获取service实例,但此时不保证onServiceConnected已经被回调。 也就是我们onCreate方法执行时onServiceConnected还没有别调用。此时当然mBoundService还为空了。
相关文章推荐
- bindService执行成功后,低概率出现onServiceConnected没有被调用
- Android AIDL bindService后不能调用onServiceConnected方法(一种情况)
- Android Service在bindService不能触发onServiceConnected方法
- bindService不能触发onServiceConnected方法
- android service bindService onServiceConnected没有调用
- Android Service在bindService不能触发onServiceConnected方法
- Service的onServiceConnected没有被调用
- bindService不能触发onServiceConnected方法的原因
- Service的onServiceConnected没有被调用
- RecyclerView方法onCreateViewHolder()和onBindViewHolder()没有调用
- Service的onServiceConnected没有被调用
- bindService不调用onServiceConnected的问题
- android 写了个service 在虚拟机测试可以启动 安装到手机后就启动不了 启动了也没有执行方法
- android 写了个service 在虚拟机测试可以启动 安装到手机后就启动不了 启动了也没有执行方法
- Android中bindService的细节之三:多次调用bindService(),为什么onBind()只执行一次?
- “Service 'Driver' could not bind on port”错误的解决方法
- android 写了个service 在虚拟机测试可以启动 安装到手机后就启动不了 启动了也没有执行方法
- Service的onServiceConnected没有被调用
- bindService不能触发onServiceConnected方法的原因
- Java.NullPointException 之 Service(业务层) 空 调DAOImpl实例方法,执行报错问题