setResult(RESULT_OK, intent)回调不执行问题

本文介绍了requestCode参数的使用规则,必须大于0;并强调了setResult(RESULT_OK,intent)方法所在的Activity启动模式必须为默认模式。这些知识点对于理解Android中权限请求及结果返回流程至关重要。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、requestCode必须大于0

2、setResult(RESULT_OK, intent)的Activity的启动模式必须是默认模式


public class MainActivity extends AppCompatActivity implements View.OnClickListener { private ActivityResultLauncher<Intent> deviceListLauncher; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_main); // 初始化设备列表的启动器 deviceListLauncher = registerForActivityResult( new ActivityResultContracts.StartActivityForResult(), result -> { if (result.getResultCode() == RESULT_OK) { Intent data = result.getData(); // 处理设备列表返回的数据 if (data != null) { String deviceAddress = data.getStringExtra(“device_address”); tv_blueTooth.setText(deviceAddress); addressmac = deviceAddress; initOPSClient(addressmac); cardConnSta = true; //setButtonEnable(); Log.d(TAG, “选择的设备地址:” + addressmac); } } }); } private void openBlueTh() { Log.d(TAG, “初始化蓝牙!”); if (!mBluetoothAdapter.isEnabled()) { Intent enableIntent = new Intent( BluetoothAdapter.ACTION_REQUEST_ENABLE); bluetoothEnableLauncher.launch(enableIntent); // 使用新API } Intent serverIntent2 = new Intent(this, DeviceListActivity.class); //startActivityForResult(serverIntent2, SETTING_BT); deviceListLauncher.launch(serverIntent2); // 使用新API }public class MainActivity extends AppCompatActivity implements View.OnClickListener { private ActivityResultLauncher<Intent> deviceListLauncher; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_main); // 初始化设备列表的启动器 deviceListLauncher = registerForActivityResult( new ActivityResultContracts.StartActivityForResult(), result -> { if (result.getResultCode() == RESULT_OK) { Intent data = result.getData(); // 处理设备列表返回的数据 if (data != null) { String deviceAddress = data.getStringExtra(“device_address”); tv_blueTooth.setText(deviceAddress); addressmac = deviceAddress; initOPSClient(addressmac); cardConnSta = true; //setButtonEnable(); Log.d(TAG, “选择的设备地址:” + addressmac); } } }); } private void openBlueTh() { Log.d(TAG, “初始化蓝牙!”); if (!mBluetoothAdapter.isEnabled()) { Intent enableIntent = new Intent( BluetoothAdapter.ACTION_REQUEST_ENABLE); bluetoothEnableLauncher.launch(enableIntent); // 使用新API } Intent serverIntent2 = new Intent(this, DeviceListActivity.class); //startActivityForResult(serverIntent2, SETTING_BT); deviceListLauncher.launch(serverIntent2); // 使用新API }deviceListLauncher.launch(serverIntent2)调用后闪退,没有结果回调,debug发现在DeviceListActivitysetResult(Activity.RESULT_OK, intent); finish();后出现闪退,请分析如何解决
03-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值