对于少于6的Android版本,只需将这些权限请求添加到清单中即可:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
但是对于Android版本6,我还需要明确请求权限以获取位置:
requestPermissions(new String[]{ Manifest.permission.ACCESS_FINE_LOCATION}, 1340);
问题是,如果我添加上面的代码,App只能在android 6上运行,并且无法在较低版本上运行,因为requestPermissions
在较低版本上不存在。如果我删除它,我将无法获得android 6上的位置。
我能做些什么才能让我的应用程序能够获得android 4.0.3 +上的位置
答案 0 :(得分:0)
您可以检查运行应用的手机的API级别,如下所示:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//later than 4.x - so here you can run the code
//ideally you want to make this call in a helper method which you then annotate with the @TargetApi
requestPermissions(new String[]{ Manifest.permission.ACCESS_FINE_LOCATION}, 1340);
}
else{
//nothing to do in your case ?
}
您还可以在要制作的方法中使用@TargetApi(Build.VERSION_CODES.LOLLIPOP)
&#34; Lollipop +&#34;调用