尽管我在方法上添加了@RequiresPermission,但Android lint仍在报告"缺少权限"。代码为:
@NonNull
@RequiresPermission(Manifest.permission.READ_CONTACTS)
public static Map<String, Integer> selectContactVersions(@Nullable Collection<String> contactIds) {
if (!PermissionUtils.isPermissonGranted(ApplicationKeeper.getApplication(), Manifest.permission.READ_CONTACTS)) {
return Collections.emptyMap();
}
错误报告为:
AddressAccesssor.java:41: 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
38 @RequiresPermission(Manifest.permission.READ_CONTACTS)
39 public static Map<String, Integer> selectContactVersions(@Nullable Collection<String> contactIds) {
40 if (!PermissionUtils.isPermissonGranted(ApplicationKeeper.getApplication(), Manifest.permission.READ_CONTACTS)) {
41 return Collections.emptyMap();
42 }
AddressAccessor.java:126: 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
123 return Collections.emptyMap();
124 }
125
126 Map<String, List<String>> contactIdPhoneListMap = selectPhones(contactIds);
127
128 Map<String, AddressbookContact> addressbookContactMap = new HashMap<String, AddressbookContact>();
129 Cursor cursor = null;
我错过什么了吗?
我错过了什么吗?
嗯,你错过了很多。
权限需要在清单中声明,并且需要在运行时中请求。仅仅压制警告是不够的。
明确请求权限以解决问题。
阅读以下文档:
https://developer.android.com/training/permissions/requesting
或者简单地使用我的图书馆。