bindService执行成功后,低概率出现onServiceConnected没有被调用

SuperCora 2014-10-09 10:27:39
如题。
即第一次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>
...全文
1057 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
村口老张头 2015-03-05
  • 打赏
  • 举报
回复 2
在OnBind()方法中需返回一个IBinder实例,不然onServiceConnected方法不会调用。
哎,真难 2014-10-11
  • 打赏
  • 举报
回复
哦,启动了service在bind么,那看下bindservice里面是否获取到了service的实例,,,
SuperCora 2014-10-11
  • 打赏
  • 举报
回复
打印service里的日志显示,异常出现时,service并没有block在某一个动作上,应该处于idle状态
SuperCora 2014-10-11
  • 打赏
  • 举报
回复
引用 10 楼 heaimnmn 的回复:
哦,启动了service在bind么,那看下bindservice里面是否获取到了service的实例,,,
看来只能这么看看了,刚把debug版本编译出来
SuperCora 2014-10-10
  • 打赏
  • 举报
回复
hello,可能我没说清楚 我的意思是说service最初是以startService的方式启动的,所以后面的bindService后,应该不会再调用onCreate 我这么说没问题吧?
哎,真难 2014-10-10
  • 打赏
  • 举报
回复
无论怎么启动service都需要启动oncreate,你不启动那里来service??
SuperCora 2014-10-10
  • 打赏
  • 举报
回复
忘了说了,这个service是startService的方式启动的

    Intent in = new Intent();
    in.putExtras(intent);
    in.setClass(context, BluetoothAvrcpCtlService.class);
    String action = intent.getAction();
    in.putExtra("action", action);

    Log.d(TAG,"Calling start service!!!! with action = " + in.getAction());
    context.startService(in);
我先说说我现在的分析结论 1.service是以startService的方式启动的,所以bindService的过程应该不会触发onCreate和onStartCommand 2.失败后的重试过程中,并没有其他行为调用stopService来结束服务。因为代码中没有stopService;调用startService的地方也只有一个,从日志上看,也只执行了一次 正常的日志如下 Line 2511: 01-02 00:06:53.340 D/BluetoothAvrcpCtlReceiver( 3062): Calling start service!!!! with action = null //zyf Line 2516: 01-02 00:06:53.370 D/BluetoothAvrcpCtlService( 3062): onCreate //zyf Line 2521: 01-02 00:06:53.380 D/BluetoothAvrcpCtlService( 3062): onStartCommand //zyf Line 2819: 01-02 00:06:54.070 D/BluetoothAvrcpCtl( 3545): Proxy object connected //zyf Line 2820: 01-02 00:06:54.070 D/BluetoothAvrcpCtl( 3545): bind successfully //zyf Line 3280: 01-02 00:07:01.200 D/BluetoothAvrcpCtl( 3545): Proxy object connected //zyf Line 3281: 01-02 00:07:01.200 D/BluetoothAvrcpCtl( 3545): bind successfully //zyf
哎,真难 2014-10-10
  • 打赏
  • 举报
回复
oncreate肯定要执行的,你debug那里看
SuperCora 2014-10-10
  • 打赏
  • 举报
回复
引用 3 楼 heaimnmn 的回复:
建议debug看看,是不是没有绑定成功,即实际没有bindservice
具体怎么看呢? 绑定的代码如下

       if (!context.bindService(new Intent(IBluetoothAvrcpCtl.class.getName()), mConnection, Context.BIND_AUTO_CREATE)) {
               Log.e(TAG, "Could not bind to Bluetooth AVRCP CT Service");
            }
            else
            {
                Log.d(TAG,"bind successfully //zyf");
            }
可以肯定,bindService肯定是执行过的
哎,真难 2014-10-10
  • 打赏
  • 举报
回复
建议debug看看,是不是没有绑定成功,即实际没有bindservice
SuperCora 2014-10-10
  • 打赏
  • 举报
回复
自己顶 有么有哪位大虾遇到过类似情况的?
SuperCora 2014-10-09
  • 打赏
  • 举报
回复
从最近抓取的日志上看,发现重试的次数不固定 Line 99: 01-02 03:24:13.880 D/BluetoothAvrcpCtl( 3492): bind successfully //zyf Line 123: 01-02 03:24:14.010 D/BluetoothAvrcpCtl( 3492): bind successfully //zyf Line 147: 01-02 03:24:14.130 D/BluetoothAvrcpCtl( 3492): bind successfully //zyf Line 171: 01-02 03:24:14.250 D/BluetoothAvrcpCtl( 3492): bind successfully //zyf Line 195: 01-02 03:24:14.370 D/BluetoothAvrcpCtl( 3492): bind successfully //zyf Line 219: 01-02 03:24:14.490 D/BluetoothAvrcpCtl( 3492): bind successfully //zyf Line 243: 01-02 03:24:14.610 D/BluetoothAvrcpCtl( 3492): bind successfully //zyf Line 267: 01-02 03:24:14.730 D/BluetoothAvrcpCtl( 3492): bind successfully //zyf Line 291: 01-02 03:24:14.860 D/BluetoothAvrcpCtl( 3492): bind successfully //zyf Line 316: 01-02 03:24:14.980 D/BluetoothAvrcpCtl( 3492): bind successfully //zyf Line 340: 01-02 03:24:15.100 D/BluetoothAvrcpCtl( 3492): bind successfully //zyf Line 365: 01-02 03:24:15.220 D/BluetoothAvrcpCtl( 3492): bind successfully //zyf Line 388: 01-02 03:24:15.330 D/BluetoothAvrcpCtl( 3492): bind successfully //zyf Line 412: 01-02 03:24:15.450 D/BluetoothAvrcpCtl( 3492): bind successfully //zyf Line 437: 01-02 03:24:15.580 D/BluetoothAvrcpCtl( 3492): bind successfully //zyf Line 461: 01-02 03:24:15.700 D/BluetoothAvrcpCtl( 3492): bind successfully //zyf Line 484: 01-02 03:24:15.820 D/BluetoothAvrcpCtl( 3492): bind successfully //zyf Line 508: 01-02 03:24:15.940 D/BluetoothAvrcpCtl( 3492): bind successfully //zyf Line 534: 01-02 03:24:16.060 D/BluetoothAvrcpCtl( 3492): bind successfully //zyf Line 558: 01-02 03:24:16.180 D/BluetoothAvrcpCtl( 3492): bind successfully //zyf Line 583: 01-02 03:24:16.380 D/BluetoothAvrcpCtl( 3492): bind successfully //zyf Line 607: 01-02 03:24:16.500 D/BluetoothAvrcpCtl( 3492): bind successfully //zyf Line 631: 01-02 03:24:16.620 D/BluetoothAvrcpCtl( 3492): bind successfully //zyf Line 655: 01-02 03:24:16.740 D/BluetoothAvrcpCtl( 3492): bind successfully //zyf Line 679: 01-02 03:24:16.870 D/BluetoothAvrcpCtl( 3492): bind successfully //zyf Line 703: 01-02 03:24:16.990 D/BluetoothAvrcpCtl( 3492): bind successfully //zyf Line 727: 01-02 03:24:17.110 D/BluetoothAvrcpCtl( 3492): bind successfully //zyf Line 751: 01-02 03:24:17.240 D/BluetoothAvrcpCtl( 3492): bind successfully //zyf Line 776: 01-02 03:24:17.360 D/BluetoothAvrcpCtl( 3492): bind successfully //zyf Line 800: 01-02 03:24:17.490 D/BluetoothAvrcpCtl( 3492): bind successfully //zyf Line 824: 01-02 03:24:17.610 D/BluetoothAvrcpCtl( 3492): bind successfully //zyf Line 848: 01-02 03:24:17.730 D/BluetoothAvrcpCtl( 3492): bind successfully //zyf Line 872: 01-02 03:24:17.850 D/BluetoothAvrcpCtl( 3492): bind successfully //zyf Line 896: 01-02 03:24:17.970 D/BluetoothAvrcpCtl( 3492): bind successfully //zyf Line 921: 01-02 03:24:18.100 D/BluetoothAvrcpCtl( 3492): bind successfully //zyf Line 945: 01-02 03:24:18.220 D/BluetoothAvrcpCtl( 3492): bind successfully //zyf Line 968: 01-02 03:24:18.330 D/BluetoothAvrcpCtl( 3492): bind successfully //zyf Line 992: 01-02 03:24:18.450 D/BluetoothAvrcpCtl( 3492): bind successfully //zyf Line 1017: 01-02 03:24:18.580 D/BluetoothAvrcpCtl( 3492): bind successfully //zyf Line 1040: 01-02 03:24:18.690 D/BluetoothAvrcpCtl( 3492): bind successfully //zyf Line 1064: 01-02 03:24:18.810 D/BluetoothAvrcpCtl( 3492): bind successfully //zyf Line 1088: 01-02 03:24:18.930 D/BluetoothAvrcpCtl( 3492): bind successfully //zyf Line 1099: 01-02 03:24:19.020 D/BluetoothAvrcpCtl( 3492): Proxy object connected //zyf 整个过程持续了约6s

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧