Android 5.1上的位置权限
作者:互联网
我们已经构建并部署了运行良好的基于位置的购物应用程序.直到我们发现我们的应用由于抛出安全异常而无法在Android 5.1.1设备上运行.
经过一番研究,我发现Google创建了一种在运行时请求权限的新方法,但没有发现任何明确的方法.
任何人都可以在运行时给出明确的指导方针/要求用户许可的代码行.该应用程序是否还需要在每次需要位置时执行此操作(在我们的例子中,位置很多);
编辑:
这是清单的一部分:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<!-- GCM Permissions -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="com.site.app.permission.C2D_MESSAGE" />
<permission android:name="com.site.app.permission.C2D_MESSAGE" android:protectionLevel="signature" />
这是Logcat:
09-16 10:08:55.531: E/AndroidRuntime(22970): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.site.app/com.site.app.Search}: java.lang.SecurityException: "passive" location provider requires ACCESS_FINE_LOCATION permission.
09-16 10:08:55.531: E/AndroidRuntime(22970): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
09-16 10:08:55.531: E/AndroidRuntime(22970): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
09-16 10:08:55.531: E/AndroidRuntime(22970): at android.app.ActivityThread.access$800(ActivityThread.java:156)
09-16 10:08:55.531: E/AndroidRuntime(22970): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
09-16 10:08:55.531: E/AndroidRuntime(22970): at android.os.Handler.dispatchMessage(Handler.java:102)
09-16 10:08:55.531: E/AndroidRuntime(22970): at android.os.Looper.loop(Looper.java:211)
09-16 10:08:55.531: E/AndroidRuntime(22970): at android.app.ActivityThread.main(ActivityThread.java:5389)
09-16 10:08:55.531: E/AndroidRuntime(22970): at java.lang.reflect.Method.invoke(Native Method)
09-16 10:08:55.531: E/AndroidRuntime(22970): at java.lang.reflect.Method.invoke(Method.java:372)
09-16 10:08:55.531: E/AndroidRuntime(22970): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
09-16 10:08:55.531: E/AndroidRuntime(22970): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
09-16 10:08:55.531: E/AndroidRuntime(22970): Caused by: java.lang.SecurityException: "passive" location provider requires ACCESS_FINE_LOCATION permission.
09-16 10:08:55.531: E/AndroidRuntime(22970): at android.os.Parcel.readException(Parcel.java:1553)
09-16 10:08:55.531: E/AndroidRuntime(22970): at android.os.Parcel.readException(Parcel.java:1505)
09-16 10:08:55.531: E/AndroidRuntime(22970): at android.location.ILocationManager$Stub$Proxy.getLastLocation(ILocationManager.java:693)
09-16 10:08:55.531: E/AndroidRuntime(22970): at android.location.LocationManager.getLastKnownLocation(LocationManager.java:1184)
09-16 10:08:55.531: E/AndroidRuntime(22970): at com.site.app.Search.getBestLocator(Search.java:423)
09-16 10:08:55.531: E/AndroidRuntime(22970): at com.site.app.Search.useAndroidFinder(Search.java:563)
09-16 10:08:55.531: E/AndroidRuntime(22970): at com.site.app.Search.onCreate(Search.java:126)
09-16 10:08:55.531: E/AndroidRuntime(22970): at android.app.Activity.performCreate(Activity.java:5990)
09-16 10:08:55.531: E/AndroidRuntime(22970): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
09-16 10:08:55.531: E/AndroidRuntime(22970): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
09-16 10:08:55.531: E/AndroidRuntime(22970): ... 10 more
解决方法:
在执行需要检查权限的代码行之前,可以使用新的ContextCompat类和方法checkSelfPermission来检查权限.
public static boolean isPermissionGranted(String permission, Context context){
//int res = ContextCompat.checkSelfPermission(context, permission);
return (ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED);
}
现在获得位置许可,您需要检查一次用户是否在您的应用程序中授予了ACCESS_COARSE_LOCATION或ACCESS_FINE_LOCATION.
标签:android-permissions,android-5-1-1-lollipop,location,android-location,android 来源: https://codeday.me/bug/20191119/2038322.html