Java代码中的Android位置权限检查
我正试图在API 23上的google MapsActivity上使用android定位服务。我在清单文件中拥有以下权限:Java代码中的Android位置权限检查,android,permissions,gps,location,maps,Android,Permissions,Gps,Location,Maps,我正试图在API 23上的google MapsActivity上使用android定位服务。我在清单文件中拥有以下权限: <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="andr
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
它显示了一个生成权限检查代码的错误(如下)。我也不知道该进入街区什么地方;似乎条件语句已经在进行必要的检查
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
似乎我在此条件中输入的任何内容都会导致错误。尝试以下操作:
if (ContextCompat.checkSelfPermission(App.getContext(),
android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= 23) { // Marshmallow
ActivityCompat.requestPermissions(getActivity(), new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION_REQUEST_CODE);
}
} else { // no need to ask for permission
// start to find location...
}
并加上:
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == LOCATION_PERMISSION_REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// start to find location...
} else { // if permission is not granted
// decide what you want to do if you don't get permissions
}
}
}
LOCATION\u PERMISSION\u REQUEST\u code
是您在类中设置的final int
(例如1252)
另请阅读,了解权限最佳做法。尝试以下方法:
if (ContextCompat.checkSelfPermission(App.getContext(),
android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= 23) { // Marshmallow
ActivityCompat.requestPermissions(getActivity(), new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION_REQUEST_CODE);
}
} else { // no need to ask for permission
// start to find location...
}
并加上:
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == LOCATION_PERMISSION_REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// start to find location...
} else { // if permission is not granted
// decide what you want to do if you don't get permissions
}
}
}
LOCATION\u PERMISSION\u REQUEST\u code
是您在类中设置的final int
(例如1252)
另请阅读,了解权限最佳做法。查看此答案中的代码:查看此答案中的代码: