LocationCallback not getting called

后端 未结 5 1158
醉酒成梦
醉酒成梦 2021-01-07 18:18

I am making a location request using FusedLocationProviderClient. I have kept a timer for 30 seconds to check if location is received within that timeframe.

相关标签:
5条回答
  • 2021-01-07 18:55

    I was facing this same issue. In my case the device-location method/accuracy was set to Phone only rather than High accuracy or Battery saving. Change the settings to High accuracy and receive callbacks on locationCallback.

    0 讨论(0)
  • 2021-01-07 19:01

    This happened to me when the location was turned off. Not the location permission for the app, but the location for the android device itself. Requesting that the user activate the GPS fixed it for me:

    protected void onCreate(Bundle savedInstanceState) {
        //...
        final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
    
        if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
            buildAlertMessageNoGps();
        }
    }
    
    private void buildAlertMessageNoGps() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Your GPS seems to be disabled, do you want to enable it?")
                .setCancelable(false)
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
                        startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                    }
                })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
                        dialog.cancel();
                    }
                });
        final AlertDialog alert = builder.create();
        alert.show();
    }
    

    Solution from here.

    You may also need to turn the GPS off and then on again to trigger a dialog to improve location accuracy:

    0 讨论(0)
  • 2021-01-07 19:09

    You need to request only the following permission:

    Manifest.permission.ACCESS_FINE_LOCATION
    

    I had the same issue and I was requesting both permissions with:

    Manifest.permission.ACCESS_COARSE_LOCATION
    

    When I removed COARSE LOCATION I got the update almost immediately.

    0 讨论(0)
  • 2021-01-07 19:11

    try to place LocationCallback to "onCreate()" method.

    0 讨论(0)
  • 2021-01-07 19:15

    Had the same issue on mobiles with Android Pie. Location permission was granted(manually by going to app settings) and location was on with high accuracy but wasn't getting location updates. Got fixed after i added code to check if location permission was granted before requesting for location updates.

    if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {
        // Permission is not granted. Request for permission
    }
    
    0 讨论(0)
提交回复
热议问题