我目前正在尝试制作位置应用。 但是,每当我尝试在方法中使用位置服务时,我对如何进行权限检查感到有点恼火。
问题是,如果我在onCreate()中请求了权限,是否有办法可以跳过权限检查?
这是我尝试调用位置服务的代码:
protected void startLocationUpdates() {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, LocationRequest mLocationRequest, this);
Location OldLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
String OldLocationTime = DateFormat.getTimeInstance().format(new Date());
}
public void onLocationChanged(Location location) {
Location NewLocation = location;
String NewLocationTime = DateFormat.getTimeInstance().format(new Date());
}
这是我的权限检查我可能需要添加到每个方法:
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
Manifest.permission.ACCESS_FINE_LOCATION)) {
GPSExplanation();
} else {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
}
}