我们在调用android拨打电话功能的时候,“startActivity(intent)”报了如下这样的错误:
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)
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
checkPermission错误如图所示:
错误分析:
从上述错误的英文可以看出,如果用户拒绝了直接拨号的权限,我们此时点击打电话,将会报错误,你该怎么处理?
其中有一个“SecurityException”的英文,它的意思就是告诉我们,如果对方没有在应用中授权“电话”权限,将会抛出SecurityException错误,所以我们知道了上面提示我们的原因之后,请看下方的解决方案!
解决方案:
使用try catch捕获抛出“SecurityException”异常即可,解决方案如下,此时startActivity就不会报错误了,如下: