美文网首页
使用 registerForActivityResult 替代

使用 registerForActivityResult 替代

作者: Obadiah | 来源:发表于2021-12-13 10:10 被阅读0次

    以开启蓝牙为例:

    private val openBLELauncher =
        registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
            Log.d("test", ": $result")
            if (result.resultCode == RESULT_OK) {
    
            }
        }
    
    
    private fun turnOnBLE() {
        val bluetoothManager =
            requireContext().getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
        val bluetoothAdapter = bluetoothManager.adapter
        if (!bluetoothAdapter.isEnabled) {
            val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
            openBLELauncher.launch(enableBtIntent)
        }
    
    }
    

    相关文章

      网友评论

          本文标题:使用 registerForActivityResult 替代

          本文链接:https://www.haomeiwen.com/subject/vxrjfrtx.html