是否可以在Android中捕获异常?

发布于 2025-02-03 06:46:48 字数 656 浏览 5 评论 0原文

最近,我修改了Android-12的Android代码。 我没有为我的API提供Android-12相关的蓝牙连接权限。因此,我按照以下操作处理:

public List<BluetoothDevice> getHfpDevices() {
    List<BluetoothDevice> hfpConnectedDevs = new ArrayList<>();
    try {
        if (mHeadsetService != null) {
            hfpConnectedDevs = mHeadsetService.getConnectedDevices();
        }
    } catch (SecurityException ex) {
        Log.e(TAG, "Security Exception in Android-12", ex);
    }
    return hfpConnectedDevs;
}

当访问此API时,这是否会抓住SecurityException?还是我需要处理异常本身?

catch (Exception ex) {

PS:我现在不想添加requestPermission Flow。我只需要解决此API时提出的例外

Recently I modified my Android code for Android-12.
I was not having android-12 related bluetooth connect permissions for my API. So I handled as below :

public List<BluetoothDevice> getHfpDevices() {
    List<BluetoothDevice> hfpConnectedDevs = new ArrayList<>();
    try {
        if (mHeadsetService != null) {
            hfpConnectedDevs = mHeadsetService.getConnectedDevices();
        }
    } catch (SecurityException ex) {
        Log.e(TAG, "Security Exception in Android-12", ex);
    }
    return hfpConnectedDevs;
}

Does this catch the securityException thrown when this API is accessed ? or do I need to handle the Exception itself ?

catch (Exception ex) {

PS : I don't want to add the requestPermission flow as of now. I just need to solve the exception raised when this API is called

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文