Call requires permission which may be rejected by user: code should explicitly check to see if permi

本文探讨了在使用特定API时遇到的权限检查问题,特别是在运行时请求READ_PHONE_STATE权限时可能出现的SecurityException。文章提供了代码示例,展示了如何通过try-catch块优雅地处理此类异常,确保应用程序的稳定性和用户体验。

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

Call requires permission which may be rejected by user: code should explicitly check to see if permission is available (with checkPermission) or explicitly handle a potential SecurityException less... (Ctrl+F1) 
Inspection info:This check scans through your code and libraries and looks at the APIs being used, and checks this against the set of permissions required to access those APIs. If the code using those APIs is called at runtime, then the program will crash.  Furthermore, for permissions that are revocable (with targetSdkVersion 23), client code must also be prepared to handle the calls throwing an exception if the user rejects the request for permission at runtime.  Issue id: MissingPermission

 

 

 

图片

 

其中

TelephonyManager tm = (TelephonyManager) getActivity().getSystemService(Context.TELEPHONY_SERVICE);

将代码替换

try{
   operatorId = tm.getSubscriberId();
}catch (SecurityException e){
   e.printStackTrace();
}

对应权限

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值