80,351
社区成员
发帖
与我相关
我的任务
分享
//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");
}
}
BluetoothAvrcpCtBinder mBinder;
@Override
public void onCreate() {
mBinder = new BluetoothAvrcpCtBinder(this);
......
}
public IBinder onBind(Intent intent) {
Log.d(TAG,"onBind");
return mBinder;
}
- <service android:name=".avrcpct.BluetoothAvrcpCtlService">
- <intent-filter>
<action android:name="android.bluetooth.IBluetoothAvrcpCtl" />
</intent-filter>
</service>
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
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肯定是执行过的