小编Ano*_*ous的帖子

startActivityForResult 已弃用,无法使用新代码请求蓝牙连接

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)

java android bluetooth

2
推荐指数
1
解决办法
3892
查看次数

标签 统计

android ×1

bluetooth ×1

java ×1