protected void onCreate(Bundle savedInstanceState) {
...
Intent intentBluetoothEnable = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityResult.launch(intentBluetoothEnable);
if(bluetooth0n) {}
else {
moveTaskToBack(true);
finishAndRemoveTask();
System.exit(0);
}
...
}
ActivityResultLauncher<Intent> startActivityResult = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK){
bluetoothon = true;
}
else {
bluetoothon = false;
}
}
}
);
As soon as the code runs, it exits immediately. When I run debug, 'startActivityResult.launch(intentBluetoothEnable);' Immediately after executing this code, a dialog asking whether to turn on Bluetooth appears, and the next exit code is executed immediately without waiting for a response.
Do I need to add a response wait code? I searched for example code, but I don't think I've seen an example with waiting code.
CodePudding user response:
Blutooth enable action is an asynchronous event, that is, unpredictable when the user presses the button exactly. That's why you don't have to finish actvity as soon as you launch the bluetooth enable intent because it will take some amount of time which is a large amount from the processor perspective. It can handle hundreds of other tasks that waiting for processing in the meantime.
What you want to achive is done by reversing the conditional logic and rearranging the code that exits from the activity. According to your code the activity must finish when user choose to not enable bluetooh. But to know the response we need to wait or the user to take action first. That's when the activity result callback is called. Hence we should exit when we get the reponse of the user NOT in onCreate.
The fixed code would look like this
protected void onCreate(Bundle savedInstanceState) {
...
BluetoothManager mBluetoothManager = getSystemService(BluetoothManager.class);
BluetoothAdapter mBluetoothAdapter = mBluetoothManager.getAdapter();
if (mBluetoothAdapter == null) {
// Device doesn't support Bluetooth, you may wanna show a warning dialog and then exit
exitOnBluetoothFail();
}
// Here check only whether the Bluetooth hardware is off
if(!mBluetoothAdapter.isEnabled()) {
// Bluetooth is off, now we should request from user to turn it on
Intent intentBluetoothEnable = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityResult.launch(intentBluetoothEnable);
}
...
}
ActivityResultLauncher<Intent> startActivityResult = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK){
// User accepted to turn the bluetooth on, handle it here not in onCreate using a boolean flag
}
else {
// User did not agree with turning on the bluetooth, so exit
exitOnBluetoothFail();
}
}
}
);
private void exitOnBluetoothFail() {
moveTaskToBack(true);
finishAndRemoveTask();
System.exit(0);
}