OnLocationChanged callback is never called

前端 未结 14 1317
星月不相逢
星月不相逢 2020-12-01 03:28

I am trying to get the users current location using the LocationManager. I have done a lot of research and can\'t seem to find anyone with the same problem. T

相关标签:
14条回答
  • As Steve Blackwell wrote, your setup is good. What you can try is to understand if you 100% have a GPS fixed signal! I have installed a nice GPS Tester application (from the play store) and it will show you if you have a fix or not and to which satellites you are connected and what is the connection strength. This was the reason my OnLocationChanged() never called.. I tried running it from within a building :\

    good luck!

    0 讨论(0)
  • 2020-12-01 03:41
    LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    if (manager != null) {
            List<String> providers = manager.getAllProviders();
            for (String provider : providers) {
               manager.requestLocationUpdates(provider, TIME_BW_UP, DISTANCE_BW_UP, locationListener);
            }
    }
    
    0 讨论(0)
  • 2020-12-01 03:41

    This should be happen due to your phone settings. In phones Location settings, If the Location service Mode set to GPS only, then OnLocationChanged callback calls when there is GPS Fix. By selecting Use GPS, WI-FI And mobile networks options, location will be determine by using those three providers. This will take less time to locate your device location. ***Path to access to location services could be change device to device. Thanks

    0 讨论(0)
  • 2020-12-01 03:42

    I had the same problem (onLocationChanged() never called with NETWORK_PROVIDER) on my real device (M, app's targetSDK 23) until I restarted it.

    0 讨论(0)
  • 2020-12-01 03:44

    Replacing

    LocationManager.GPS_PROVIDER

    with

    LocationManager.NETWORK_PROVIDER

    solved my problem.

    Here is code snippet of my location manager

     LocationManager locationManager = (LocationManager) getActivity().getSystemService(LOCATION_SERVICE);
    

    //Check for Location permissions, Marshmallow and above

            if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                    && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
    
                Toast.makeText(getActivity(), "Not Enough Permission", Toast.LENGTH_SHORT).show();
                return;
            }
    

    //Get current location to start with

            Location myLocation = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
    
            currentLatitude = myLocation.getLatitude();
            currentLongitude = myLocation.getLongitude();
    

    // Request location update using LocationManager.NETWORK_PROVIDER

            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
                    MIN_TIME_BW_UPDATES,
                    MIN_DISTANCE_CHANGE_FOR_UPDATES, MapFragment.this);
    
    0 讨论(0)
  • 2020-12-01 03:44

    Sorry for bumping such an old thread, but this might come handy to some others, since this question is ranking relatively high on google search.

    I had a similar problem, the onlocationchanged wasn't getting fired, but I could not find anything wrong with my code. After trying all sorts of combination, I realised that the root cause of the problem was the fact that I was registering the listener in a service.

    The service WAS a foreground service, so based on Google documentation that should work, it DID NOT!

    Wrapping the initialisation in a method, and calling the method from the activity which created or bind to the service solved my problem.

    This is my method in my service:

    void start_gps_track(int i) {
            if (myCarLocationListener!=null) {
                if (i==1) //if start night
                    myCarLocationListener.setNightstart(true);
                else if (i==2) //if start GPS
                    myCarLocationListener.setLocationStart(true);
                return;
            }
            locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            if ( locationManager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
                myCarLocationListener=new CarLocationListener(this,adjust_audio);
                if (i==1) //if start night
                    myCarLocationListener.setNightstart(true);
                else if (i==2) //if start GPS
                    myCarLocationListener.setLocationStart(true);
                try {
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 0, myCarLocationListener);
                    Log.d("HU","Registered location listener");
                    Location lastlocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (lastlocation == null)
                        lastlocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    if (lastlocation != null)
                        myCarLocationListener.onLocationChanged(lastlocation);
                }
                catch (SecurityException e){
                    Log.e("HU-GPS","No permission to access Location");
                }
            }
            else
            {
                Log.e("HU-GPS","No GPS provider or no permission");
            }
        }
    

    If I call this from within the Service itself I won't receive any Location Updates (again note service is a FOREGROUND service and it's actually binded to an activity which is currently running).

    However if I call this method from the activity which created/bound to the service like below it works fine:

    @Override
    public void onServiceConnected(ComponentName className,
                                   IBinder service) {
        // We've bound to LocalService, cast the IBinder and get LocalService instance
        TransporterService.LocalBinder binder = (TransporterService.LocalBinder) service;
        mService = binder.getService();
        mService.updatePlayer(player.this);
        try {
            mService.start_gps_track(0);
        }
        catch (Exception e) {            //This should only happen if there is no GPS provider available, but it might happen quickly after boot when the device is not yet ready.
            mService.recheck_gps(0);
        }
    
        mBound = true;
      }
    
    @Override
    public void onServiceDisconnected(ComponentName arg0) {
        mBound = false;
    
    }
    };
    

    Hope this helps some others who come across this problem.

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