当扩展 BluetoothGattCallback
时,实现者应该调用 super 方法吗?
例子:
public void onCharacteristicWrite(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,
int status) {
// Is this needed?
super.onCharacteristicWrite(gatt, characteristic, status);
...
}
最佳答案
在这种情况下,似乎没有必要调用 super 方法。
BluetoothGattCallback
类是抽象的,它的所有方法都是空的。
编辑:
BluetoothGattCallback
是 Android SDK 的一部分,位于 native 层之上。
它的方法是从 BluetoothGatt
类调用的,该类处理来自 native 层的回调并将它们转换为对 BluetoothGattCallback
方法的调用。
如果您想知道为什么 BluetoothGattCallback
是一个具有空实现的抽象类而不是接口(interface)。可能是因为它有太多的回调方法,所以实现一个接口(interface)会导致代码不必要的膨胀。
编辑2:
这方面的官方引用并不难获得。 Compatiblity definition硬件制造商声明他们必须以 Android SDK 中声明的形式实现 Android API。
注意底层native层是通过Android的Binder机制连接起来的。
关于Android BluetoothGatt super 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36768916/