我现在正在研究蓝牙 Android API,并且遇到了 BluetoothChat 示例。 http://developer.android.com/resources/samples/BluetoothChat/index.html
它包含许多错误,首先是它使用 API 11 的简单事实,但清单并未强制使用此最低 API。
其他有趣的事情是在 Activity 生命周期方法上使用 synchronized 关键字,例如 onResume:
@Override
public synchronized void onResume() {
super.onResume();
if(D) Log.e(TAG, "+ ON RESUME +");
// Performing this check in onResume() covers the case in which BT was
// not enabled during onStart(), so we were paused to enable it...
// onResume() will be called when ACTION_REQUEST_ENABLE activity returns.
if (mChatService != null) {
// Only if the state is STATE_NONE, do we know that we haven't started already
if (mChatService.getState() == BluetoothChatService.STATE_NONE) {
// Start the Bluetooth chat services
mChatService.start();
}
}
}
为什么这里使用这个关键字?是否有任何合理的解释,或者只是编写代码的人不知道 onResume 将始终由同一个线程调用?还是我错过了什么?
先感谢您!