我该如何处理蓝牙启用对话框中的 "拒绝" 按钮按下事件?我尝试使用 OnDismissListener
和 OnCancelListener
,甚至尝试了 onActivityResult
,但并没有起作用。以下是代码:
private BluetoothAdapter mBluetoothAdapter;
private static final int REQUEST_ENABLE_BT = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (isBleSupportedOnDevice()) {
initializeBtComponent();
} else {
finish();
}
}
private boolean isBleSupportedOnDevice() {
if (!getPackageManager().hasSystemFeature(
PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this, "BLE is not supported in this device.",
Toast.LENGTH_SHORT).show();
return false;
}
return true;
}
private void initializeBtComponent() {
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
}
@Override
protected void onResume() {
super.onResume();
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
这段代码会显示一个对话框,直到用户按下“允许”或“确定”按钮,但当他按下“拒绝”或“取消”按钮后,我想要回到之前的活动。我该怎么做?是否有任何函数在按下“拒绝”按钮时被调用?