Android Location return null after Enabling GPS in dialog

冷暖自知 提交于 2019-12-03 16:03:43

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.

Add this override method for me its worked fine--

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!