1 回答
TA贡献1951条经验 获得超3个赞
由于您mBoundLeft在onCreate()方法内部进行测试并绑定服务onStart()并取消绑定onStop()(如果您的应用程序被终止,您也不会保存瞬态状态),您将永远不会看到是否mBoundLeft为真(检查活动生命周期)。您应该测试服务是否已在内部绑定onServiceConnected(),例如使用log.d. 你应该做这样的事情:
public void onServiceConnected(ComponentName className, IBinder service) {
// Because we have bound to an explicit
// service that is running in our own process, we can
// cast its IBinder to a concrete class and directly access it.
BluetoothLeServiceForLeft.LocalBinder binder =
(BluetoothLeServiceForLeft.LocalBinder) service;
mBluetoothLeServiceForLeft = binder.getService();
mBoundLeft = true;
Log.d("ServiceConnection", "Service is connected");
}
我建议您学习Log在 Android 中使用类进行调试,它在 Android 开发中更好(并且是标准)。
添加回答
举报