Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
Run Code Online (Sandbox Code Playgroud)
我请求使用上面的源代码启动蓝牙。但是,startActivityForResult已被弃用。所以,我正在寻找新的代码来处理这个问题。这是我找到的解决方案。
// You can do the assignment inside onAttach or onCreate, i.e, before the activity is displayed
ActivityResultLauncher<Intent> someActivityResultLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK) {
// There are no request codes
Intent data = result.getData();
doSomeOperations();
}
}
});
public void openSomeActivityForResult() {
Intent intent = new Intent(this, SomeActivity.class);
someActivityResultLauncher.launch(intent);
}
Run Code Online (Sandbox Code Playgroud)
我将其添加到我的源代码中。
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); …Run Code Online (Sandbox Code Playgroud)