bindService执行成功后,低概率出现onServiceConnected没有被调用
即第一次bindService->unbindService后,接着再执行bindService
执行完后,从bindService返回结果来看,正常。但是onServiceConnected没有被调用
采用重试的方法,重试5到10次后,绑定成功,具体是
bindService后,判断onServiceConnected是否执行,如果没有执行,先执行unBindService,然后重新执行bindService
直到onServiceConnected被成功执行
关键代码如下
Client
//getBluetoothAvrcpCtl由一activity调用
public static synchronized BluetoothAvrcpCtl getBluetoothAvrcpCtl(Context mContext) {
if(sAvrcpCtl == null)
{
sAvrcpCtl = new BluetoothAvrcpCtl(mContext);
Log.e(TAG,"getBluetoothAvrcpCtl sAvrcpCtl:"+ sAvrcpCtl);
}
return sAvrcpCtl;
}
public BluetoothAvrcpCtl(Context mContext) {
context = mContext;
if(mService == null)
{
if (!context.bindService(new Intent(IBluetoothAvrcpCtl.class.getName()), mConnection, 0)) {
Log.e(TAG, "Could not bind to Bluetooth AVRCP CT Service");
}
}
}
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
Log.d(TAG, "Proxy object connected");
mService = IBluetoothAvrcpCtl.Stub.asInterface(service);
notifyServiceBind(true);
}
public void onServiceDisconnected(ComponentName className) {
Log.d(TAG, "Proxy object disconnected");
notifyServiceBind(false);
mService = null;
}
};
public void closeProxy() {
if((mCallbacks.isEmpty()) && (mConnection != null)){
context.unbindService(mConnection);
sAvrcpCtl = null;
} else {
Log.d(TAG, "Either Callback not present or not connected to service");
}
}
AIDL文件省略
Service端
BluetoothAvrcpCtBinder mBinder;
@Override
public void onCreate() {
mBinder = new BluetoothAvrcpCtBinder(this);
......
}
public IBinder onBind(Intent intent) {
Log.d(TAG,"onBind");
return mBinder;
}
XML文件
- <service android:name=".avrcpct.BluetoothAvrcpCtlService">
- <intent-filter>
<action android:name="android.bluetooth.IBluetoothAvrcpCtl" />
</intent-filter>
</service>
相关推荐
BindService
Android四大组件之绑定Service详解,示例代码:附带博客地址: https://blog.csdn.net/lijianbiao0/article/details/85067100
Android中bindService基本使用方法概述
Android中有两种主要方式使用Service,通过调用Context的startService方法或调用Context的bindService方法,本文只探讨纯bindService的使用,不涉及任何startService方法调用的情况。如果想了解startService相关的使用,请参见《Android中startService基本使用方法概述》。 bindService启动服务的特点 相比于用startService启动的Service,bindService启动的服务具有如下特点: 1. bindService启动的服务在调用者和服务之间是典型的client-server的接口,
android bindservice
后台服务,当系统推出,关闭后台服务 有问题来这里问我
Android bindService详解[项目代码]
本文详细介绍了Android中bindService的使用方法及其生命周期。通过bindService启动的服务具有client-server接口特性,允许客户端直接调用Service中的方法,实现灵活交互。文章对比了bindService与startService的区别,包括生命周期管理、回调方法及跨进程通信能力。通过代码示例展示了同一App内client与Service的绑定流程,包括Service的onBind实现、客户端ServiceConnection的使用,以及三种典型测试流程下的生命周期回调验证。最后总结了bindService的完整生命周期流程图,帮助开发者深入理解其工作原理。
AIDL bindservice流程图
aidl binserive的流程图 内含png oom两个原文。http://blog.csdn.net/qfanmingyiq/article/details/78094417



