• 注册
当前位置:1313e > 默认分类 >正文

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;@Overridepublic void onCreate() {mBinder = new BluetoothAvrcpCtBinder(this);......}public IBinder onBind(Intent intent) {Log.d(TAG,"onBind");return mBinder;}


XML文件
 

============解决方案1============


建议debug看看,是不是没有绑定成功,即实际没有bindservice

转载于:https://www.cnblogs.com/meizhenfen42/p/4030391.html

本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 162202241@qq.com 举报,一经查实,本站将立刻删除。

最新评论

欢迎您发表评论:

请登录之后再进行评论

登录