Android Location return null after Enabling GPS in dialog

后端 未结 3 1851
一整个雨季
一整个雨季 2021-02-06 06:55

When I enter a screen, I check for if GPS is turned on, if not, the dialog to enable GPS is shown. When user clicks Yes, onActivityResult -> GPS is turned on and I try to get th

相关标签:
3条回答
  • 2021-02-06 07:43

    Add this override method for me its worked fine--

    @Override public void onLocationChanged(Location location) { getLocation("onLocationChanged"); }

    0 讨论(0)
  • 2021-02-06 07:53

    LocationSettingsRequest is used just make location settings adequate, for example you want receive more accurate (HIGH_ACCURACY) locations, then you need GPS is enabled. So in that case, LocationSettingsRequest prompts the dialog to allow api change settings.

    After pressing OK, you can see that GPS is enabled. But it does not mean you are granted to make location request yet.

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case REQUEST_CHECK_SETTINGS:
                switch (resultCode) {
                    case Activity.RESULT_OK:
                    // Here means required settings are adequate.
                    // We can make location request.
                    // But are we granted to use request location updates ?
                    break;
                }
                break;
            }
        }
    }
    

    Thats okay, but you are checking the permission, but you did never make requestPermissions. This is the first possible reason of getting null.

    public Location getLocation() {
        if (ContextCompat.checkSelfPermission(activity, 
            Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            ...
        }
        return location;
    }
    

    Even you are previously granted to make location request

      location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
      location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    

    getLastKnownLocation() method could return null, this is not unexpected behaviour. This is the second possible reason of getting null

    You are making location request, but the global location variable is never assigned in onLocationChanged callback.

     @Override
    public void onLocationChanged(Location location) {
        // global location variable is not assigned ?
        // getLocation() method will return null if getLastKnownLocation()
        // did not return null previously. 
    }
    

    This is third possible reason of getting null.

    0 讨论(0)
  • 2021-02-06 07:53

    Finally, I find a solution: I create a thread and wait for my location available.

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode){
            case REQUEST_LOCATION:
                switch (resultCode){
                    case Activity.RESULT_OK:
                        mHandler = new Handler();
                        runnable = new Runnable() {
                            @Override
                            public void run() {
                                mHandler.postDelayed(this,1000);
                                checkLocationAvailable();
                            }
                        };
                        mHandler.postDelayed(runnable,1000);
                        break;
                    case Activity.RESULT_CANCELED:
                        break;
                }
        }
    }
    

    Create a function to stop the thread when my location available.

    private void checkLocationAvailable(){
      if (mMap.getMyLocation() != null) {
      mHandler.removeCallbacks(runnable);
      // do stuff
      }
    }
    

    It is not a good solution, but hope it will help.

    0 讨论(0)
提交回复
热议问题