如何从startActivityForResult更改为registerForActivityResult此蓝牙活动?请帮助我,因为我是新的java和Android工作室。我试着看视频和教程,但我只得到更多的错误。如果您能给我一些建议,我将不胜感激。
//on btn click
mOnBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!mBlueAdapter.isEnabled()){
showToast("Turning On Bluetooth...");
//intent to on bluetooth
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, REQUEST_ENABLE_BT);
}
else {
showToast("Bluetooth is already on");
}
}
});
private val launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result: ActivityResult ->
if (result.resultCode == Activity.RESULT_OK) {
}
}
launcher.launch(intent)
对于那些需要的Java可以使用
private ActivityResultLauncher<Intent> startForResult =
registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
});
startForResult.launch(intent);