我正在研究藍牙Android API,並且我遇到了BluetoothChat示例。 http://developer.android.com/resources/samples/BluetoothChat/index.htmlBluetoothChat同步onResume Activity生命週期方法,爲什麼?
它包含許多錯誤,首先它使用API 11但清單並不強制這個最小API的簡單事實。
其他有趣的事情是在活動生命週期方法的使用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總是會被同一個線程調用?或者我想念什麼?
預先感謝您!
我真的很想知道這個問題的答案以及... – 2012-02-04 18:53:17