ContextCompat.checkSelfPermission无效的问题

本文探讨了在Android应用中遇到的权限检查问题,当targetSdkVersion小于23时,ContextCompat.CheckSelfPermission和Context.checkSelfPermission方法会始终返回PERMISSION_GRANTED。文章提供了使用PermissionChecker.checkSelfPermission替代的方法来正确检查运行时权限。

最近发现一个问题,我在使用ContextCompat.CheckSelfPermission()时无论如何开关权限返回值都是PackageManager.PERMISSION_GRANTED,而使用PackageManager.checkPermission()的时候返回值又始终都是PackageManager.PERMISSION_DENIED;

经过多番尝试和查找资料发现原因:

If your application is targeting an API level before 23 (Android M) then both:ContextCompat.CheckSelfPermission and Context.checkSelfPermission doesn't work and always returns 0 (PERMISSION_GRANTED). Even if you run the application on Android 6.0 (API 23).

在targetSdkVersion小于23(Android M)的时候,ContextCompat.CheckSelfPermission 和Context.checkSelfPermission方法都不能正常工作并且始终返0(PERMISSION_GRANTED),即使你的应用运行在Android6.0(API 23)的设备上。

解决办法:

As I said in the 1st point, if you targeting an API level before 23 on Android 6.0 then ContextCompat.CheckSelfPermission and Context.checkSelfPermission doesn't work. Fortunately you can use PermissionChecker.checkSelfPermission to check run-time permissions.  

可以使用PermissionChecker.checkSelfPermission()去检查权限是否被授予。



转自:https://www.jianshu.com/p/2a452546cc1f

private void updateSunriseAndSunset() { AvatrLog.i(TAG, "updateSunriseAndSunset: mLongitude = " + mLongitude + ", mLatitude = " + mLatitude); if (mLongitude == -181) { if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return; } Location lastKnownLocation = mLocationManager. getLastKnownLocation(LocationManager.GPS_PROVIDER); AvatrLog.i(TAG, "updateSunriseAndSunset: lastKnownLocation = " + lastKnownLocation); if (lastKnownLocation != null) { mLongitude = lastKnownLocation.getLongitude(); mLatitude = lastKnownLocation.getLatitude(); } else { return; } } SunriseAndSunsetBean sunriseAndSunsetBean = new SunriseAndSunsetBean(); double sunState = Math.tan(degreeToRadians(getSunDeclination())) * Math.tan(degreeToRadians(mLatitude)); if (sunState > 1) { sunriseAndSunsetBean.setSunState(SUN_STATE_POLAR_DAY); } else if (sunState < -1) { sunriseAndSunsetBean.setSunState(SUN_STATE_POLAR_NIGHT); } else { sunriseAndSunsetBean.setSunState(SUN_STATE_NORMAL); } double acos = Math.acos(-sunState); //日出时间 = 24 * (180 + 时区*15 - 经度 - arccos(-tan(太阳赤纬) * tan(纬度))) / 360 double sunriseHour = 24 * (180 + getTimeZoneNum() * 15 - mLongitude - radiansToDegree(acos)) / 360; //日落时间 = 24 * (180 + 时区*15 - 经度 + arccos(-tan(太阳赤纬) * tan(纬度))) / 360 double sunsetTime = 24 * (180 + getTimeZoneNum() * 15 - mLongitude + radiansToDegree(acos)) / 360; sunriseAndSunsetBean.setSunriseTime(sunTimeFormat(sunriseHour)); sunriseAndSunsetBean.setSunsetTime(sunTimeFormat(sunsetTime)); String feedback = new Gson().toJson(sunriseAndSunsetBean); System.setProperty("persist.sys.time.sunriseAndSunset", feedback); TimeTopicProvider.getInstance().publish(TimeTopicProvider.SUNRISE_AND_SUNSET_HOUR_TOPIC, feedback); } @Test public void testUpdateSunriseAndSunset() throws Exception { try (MockedStatic<ContextCompat> contextCompat = mockStatic(ContextCompat.class)){ contextCompat.when(() -> ContextCompat.checkSelfPermission(mockContext, Manifest.permission.ACCESS_COARSE_LOCATION)) .thenReturn(PackageManager.PERMISSION_GRANTED); contextCompat.when(() -> ContextCompat.checkSelfPermission(mockContext, Manifest.permission.ACCESS_FINE_LOCATION)) .thenReturn(PackageManager.PERMISSION_GRANTED); // when(mockLocationManager.getLastKnownLocation(Context.LOCATION_SERVICE)) // .thenReturn(Mockito.mock(Location.class)); invokePrivateMethod("updateSunriseAndSunset", null); } }结合代码帮忙补充单元测试,要求全分支覆盖
最新发布
10-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值