bindService执行成功后,低概率出现onServiceConnected没有被调用解决方法
bindService执行成功后,低概率出现onServiceConnected没有被调用
如题。
即第一次bindService->unbindService后,接着再执行bindService
执行完后,从bindService返回结果来看,正常。但是onServiceConnected没有被调用
采用重试的方法,重试5到10次后,绑定成功,具体是
bindService后,判断onServiceConnected是否执行,如果没有执行,先执行unBindService,然后重新执行bindService
直到onServiceConnected被成功执行
关键代码如下
Client
AIDL文件省略
Service端
XML文件
------解决思路----------------------
建议debug看看,是不是没有绑定成功,即实际没有bindservice
如题。
即第一次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>
------解决思路----------------------
建议debug看看,是不是没有绑定成功,即实际没有bindservice