Java Android位置权限代码无法正常工作

Java Android位置权限代码无法正常工作,java,android,google-maps,location,geocoding,Java,Android,Google Maps,Location,Geocoding,我编写了一个简单的代码,用于在运行时从用户处获取位置权限仅在第一次正常工作。之后,权限对话框根本不会出现。有人能告诉我代码中缺少了什么吗 public static final String FINE_LOCATION=Manifest.permission.ACCESS_FINE_LOCATION; public static final String COARSE_LOCATION=Manifest.permission.ACCESS_COARSE_LOCATION; p

我编写了一个简单的代码,用于在运行时从用户处获取位置权限仅在第一次正常工作。之后,权限对话框根本不会出现。有人能告诉我代码中缺少了什么吗

  public static final String FINE_LOCATION=Manifest.permission.ACCESS_FINE_LOCATION;
    public static final String COARSE_LOCATION=Manifest.permission.ACCESS_COARSE_LOCATION;
    private static final int LOCATION_PERMISSION_REQUEST_CODE=1234;
    private boolean mLocationPermissionGranted=false;

  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        getLocationPermission();
}
    public void initMap() {

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        Toast.makeText(this, "Map is ready", Toast.LENGTH_SHORT).show();
    }
 public void getLocationPermission(){
        String [] permissions={Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION};
        if(ContextCompat.checkSelfPermission(this.getApplicationContext(),FINE_LOCATION)==PackageManager.PERMISSION_GRANTED){
            if(ContextCompat.checkSelfPermission(this.getApplicationContext(),COARSE_LOCATION)==PackageManager.PERMISSION_GRANTED){
                mLocationPermissionGranted=true;
                initMap();

            } else {
                ActivityCompat.requestPermissions(this,permissions,LOCATION_PERMISSION_REQUEST_CODE);
             }
            }
            else {
            ActivityCompat.requestPermissions(this,permissions,LOCATION_PERMISSION_REQUEST_CODE);
        }

    }

 @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        mLocationPermissionGranted=false;

        switch (requestCode){
            case LOCATION_PERMISSION_REQUEST_CODE:{
                if(grantResults.length>0){
                    for(int i=0;i<grantResults.length;i++){
                        if(grantResults[i]!=PackageManager.PERMISSION_GRANTED){
                            mLocationPermissionGranted=false;
                            return;
                        }
                    }
                    mLocationPermissionGranted=true;
                    initMap();
                }
            }
        }
    }
我是谷歌地图编码新手,所以我对它知之甚少。任何人都有办法,请帮帮我。提前谢谢

这是它第一次正常工作。在得到许可之后 对话框根本不出现

一旦授予权限,则无需再次允许,除非用户通过设置应用程序更改权限或重新安装应用程序等

这是它第一次正常工作。在得到许可之后 对话框根本不出现


一旦授予权限,则无需再次允许,除非用户通过设置应用程序更改权限或重新安装应用程序等。您可以使用以下代码:

public static double longitude = 0.0d;
public static double latitude = 0.0d;
private FusedLocationProviderClient mFusedLocationClient;
private long UPDATE_INTERVAL = 30 * 1000;  /* 10 secs */
private long FASTEST_INTERVAL = 2000; /* 2 sec */
protected void startLocationUpdates() {

    LocationRequest mLocationRequest = new LocationRequest();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setInterval(UPDATE_INTERVAL);
    mLocationRequest.setFastestInterval(FASTEST_INTERVAL);

    // Create LocationSettingsRequest object using location request
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
    builder.addLocationRequest(mLocationRequest);
    LocationSettingsRequest locationSettingsRequest = builder.build();

    // Check whether location settings are satisfied
    // https://developers.google.com/android/reference/com/google/android/gms/location/SettingsClient
    SettingsClient settingsClient = LocationServices.getSettingsClient(this);
    settingsClient.checkLocationSettings(locationSettingsRequest);

    final LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    if( !locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){

        showGPSDisabledAlertToUser();

    } 
        return;
    else {


        // new Google API SDK v11 uses getFusedLocationProviderClient(this)
        LocationServices.getFusedLocationProviderClient(this).requestLocationUpdates(mLocationRequest, new LocationCallback() {
                    @Override
                    public void onLocationResult(LocationResult locationResult) {
                        // do work here
                        onLocationChanged(locationResult.getLastLocation());
                    }
                },
                Looper.myLooper());

    }
}

在oncreate中添加以下代码:

public static double longitude = 0.0d;
public static double latitude = 0.0d;
private FusedLocationProviderClient mFusedLocationClient;
private long UPDATE_INTERVAL = 30 * 1000;  /* 10 secs */
private long FASTEST_INTERVAL = 2000; /* 2 sec */
protected void startLocationUpdates() {

    LocationRequest mLocationRequest = new LocationRequest();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setInterval(UPDATE_INTERVAL);
    mLocationRequest.setFastestInterval(FASTEST_INTERVAL);

    // Create LocationSettingsRequest object using location request
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
    builder.addLocationRequest(mLocationRequest);
    LocationSettingsRequest locationSettingsRequest = builder.build();

    // Check whether location settings are satisfied
    // https://developers.google.com/android/reference/com/google/android/gms/location/SettingsClient
    SettingsClient settingsClient = LocationServices.getSettingsClient(this);
    settingsClient.checkLocationSettings(locationSettingsRequest);

    final LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    if( !locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){

        showGPSDisabledAlertToUser();

    } 
        return;
    else {


        // new Google API SDK v11 uses getFusedLocationProviderClient(this)
        LocationServices.getFusedLocationProviderClient(this).requestLocationUpdates(mLocationRequest, new LocationCallback() {
                    @Override
                    public void onLocationResult(LocationResult locationResult) {
                        // do work here
                        onLocationChanged(locationResult.getLastLocation());
                    }
                },
                Looper.myLooper());

    }
}

您可以使用以下代码:

public static double longitude = 0.0d;
public static double latitude = 0.0d;
private FusedLocationProviderClient mFusedLocationClient;
private long UPDATE_INTERVAL = 30 * 1000;  /* 10 secs */
private long FASTEST_INTERVAL = 2000; /* 2 sec */
protected void startLocationUpdates() {

    LocationRequest mLocationRequest = new LocationRequest();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setInterval(UPDATE_INTERVAL);
    mLocationRequest.setFastestInterval(FASTEST_INTERVAL);

    // Create LocationSettingsRequest object using location request
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
    builder.addLocationRequest(mLocationRequest);
    LocationSettingsRequest locationSettingsRequest = builder.build();

    // Check whether location settings are satisfied
    // https://developers.google.com/android/reference/com/google/android/gms/location/SettingsClient
    SettingsClient settingsClient = LocationServices.getSettingsClient(this);
    settingsClient.checkLocationSettings(locationSettingsRequest);

    final LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    if( !locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){

        showGPSDisabledAlertToUser();

    } 
        return;
    else {


        // new Google API SDK v11 uses getFusedLocationProviderClient(this)
        LocationServices.getFusedLocationProviderClient(this).requestLocationUpdates(mLocationRequest, new LocationCallback() {
                    @Override
                    public void onLocationResult(LocationResult locationResult) {
                        // do work here
                        onLocationChanged(locationResult.getLastLocation());
                    }
                },
                Looper.myLooper());

    }
}

在oncreate中添加以下代码:

public static double longitude = 0.0d;
public static double latitude = 0.0d;
private FusedLocationProviderClient mFusedLocationClient;
private long UPDATE_INTERVAL = 30 * 1000;  /* 10 secs */
private long FASTEST_INTERVAL = 2000; /* 2 sec */
protected void startLocationUpdates() {

    LocationRequest mLocationRequest = new LocationRequest();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setInterval(UPDATE_INTERVAL);
    mLocationRequest.setFastestInterval(FASTEST_INTERVAL);

    // Create LocationSettingsRequest object using location request
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
    builder.addLocationRequest(mLocationRequest);
    LocationSettingsRequest locationSettingsRequest = builder.build();

    // Check whether location settings are satisfied
    // https://developers.google.com/android/reference/com/google/android/gms/location/SettingsClient
    SettingsClient settingsClient = LocationServices.getSettingsClient(this);
    settingsClient.checkLocationSettings(locationSettingsRequest);

    final LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    if( !locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){

        showGPSDisabledAlertToUser();

    } 
        return;
    else {


        // new Google API SDK v11 uses getFusedLocationProviderClient(this)
        LocationServices.getFusedLocationProviderClient(this).requestLocationUpdates(mLocationRequest, new LocationCallback() {
                    @Override
                    public void onLocationResult(LocationResult locationResult) {
                        // do work here
                        onLocationChanged(locationResult.getLastLocation());
                    }
                },
                Looper.myLooper());

    }
}

public void onLocationChanged(Location-Location){//New-Location现在已确定String msg=“Updated-Location:”+Double.toString(Location.getLatitude())+“,“+Double.toString(Location.getLatitude());latitude=Location.getLatitude();longide=Location.getLatitude()();}private void showGPSDisabledAlertToUser(){Intent callGPSSettingcontent=new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_Settings);startActivity(callGPSSettingcontent);}public void onLocationChanged(LOCATION-LOCATION){//New location现已确定String msg=“更新的位置:“+Double.toString(location.getLatitude())+”,“+Double.toString(location.getLatitude());latitude=location.getLatitude();latitude=location.getLatitude();}私有void ShowGPSDisableAlertToUser(){Intent callgpssettingcontent=newintent(android.provider.Settings.ACTION\u LOCATION\u SOURCE\u Settings);startActivity(callgpssettingcontent);}