bindService执行成功后,低概率出现onServiceConnected没有被调用
生活随笔
收集整理的這篇文章主要介紹了
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文件
-?<service?android:name=".avrcpct.BluetoothAvrcpCtlService">-?<intent-filter><action?android:name="android.bluetooth.IBluetoothAvrcpCtl"?/>?</intent-filter></service>
============解決方案1============
建議debug看看,是不是沒有綁定成功,即實際沒有bindservice
轉載于:https://www.cnblogs.com/meizhenfen42/p/4030391.html
總結
以上是生活随笔為你收集整理的bindService执行成功后,低概率出现onServiceConnected没有被调用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 解剖SQLSERVER 第四篇 Orc
- 下一篇: ASP.NET-FineUI开发实践-9