permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED&&& ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION)== PackageManager.PERMISSION_GRANTED){
}
您还应该写入一个else语句请求代码,如果用户没有得到它,它将向用户请求许可。
以下是一个很好的请求权限方法:
public void requestPermissions(List< String>权限,ActivityCompat.OnRequestPermissionsResultCallback onRequestPermissionsResultCallback){
String [] params = permissions.toArray(new String [permissions.size()]);
requestPermissions(params,onRequestPermissionsResultCallback);
$ / code>
在您的情况下,您必须将位置权限传递给您使用此方法:
私人列表< String> getRequiredLocationPermissions(){
String accessCoarsePermission = android.Manifest.permission.ACCESS_COARSE_LOCATION;
String accessFineLocationPermission = android.Manifest.permission.ACCESS_FINE_LOCATION;
int hasCoarsePermission = ContextCompat.checkSelfPermission(getActivity(),accessCoarsePermission);
int hasFineLocationPermission = ContextCompat.checkSelfPermission(getActivity(),accessFineLocationPermission);
列表< String> permissions = new ArrayList<>();
if(hasCoarsePermission!= PackageManager.PERMISSION_GRANTED){
permissions.add(accessCoarsePermission);
if(hasFineLocationPermission!= PackageManager.PERMISSION_GRANTED){
permissions.add(accessFineLocationPermission);
}
返回权限;
}
i want to use GPS in my App. but when i tried to retrieve GPS readings as shown belwo in the code, Android studio gave me the belwo mentioned error
despit all the required permissions are added to manifest file
code:
LocationManager lm = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Log.w(TAG, "onLocationChanged");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.w(TAG, "onStatusChanged");
}
@Override
public void onProviderEnabled(String provider) {
Log.w(TAG, "onProviderEnabled");
}
@Override
public void onProviderDisabled(String provider) {
Log.w(TAG, "onProviderDisabled");
}
};
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, ll);//error at this line
解决方案
This is due to in Marshmallow (Android version 6.0) Users can choose to turn off permissions. Simply wrap this check to see if the permission is enabled before your location code.
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
}
You should also write in an else statement request code which will request the permission from the user if they haven't got it.
Here's a nice request permissions method:
public void requestPermissions(List<String> permissions, ActivityCompat.OnRequestPermissionsResultCallback onRequestPermissionsResultCallback) {
String[] params = permissions.toArray(new String[permissions.size()]);
requestPermissions(params, onRequestPermissionsResultCallback);
}
In your case you will have to pass it the Location permissions which you can get with this method:
private List<String> getRequiredLocationPermissions() {
String accessCoarsePermission = android.Manifest.permission.ACCESS_COARSE_LOCATION;
String accessFineLocationPermission = android.Manifest.permission.ACCESS_FINE_LOCATION;
int hasCoarsePermission = ContextCompat.checkSelfPermission(getActivity(), accessCoarsePermission);
int hasFineLocationPermission = ContextCompat.checkSelfPermission(getActivity(), accessFineLocationPermission);
List<String> permissions = new ArrayList<>();
if (hasCoarsePermission != PackageManager.PERMISSION_GRANTED) {
permissions.add(accessCoarsePermission);
}
if (hasFineLocationPermission != PackageManager.PERMISSION_GRANTED) {
permissions.add(accessFineLocationPermission);
}
return permissions;
}
这篇关于不能读取GPS读数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!