1

I have a service in which I need to run onLocationChanged. When I try to put the onLocationChanged method inside onStartCommand, it doesn't work. How can I set onLocationChanged to run when my service starts?

public class myService extends Service implements LocationListener {
Double lat;
Double lng;

@Override
public IBinder onBind(Intent arg0) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // Let it continue running until it is stopped.
    Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();





    return START_STICKY;

}

@Override
public void onDestroy() {
    super.onDestroy();
    Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();

}



 @Override
 public void onLocationChanged(Location location) {

    if (MainActivity.location != null) {


       lat = MainActivity.location.getLatitude();

        lng = MainActivity.location.getLongitude();


        if (MainActivity.placeArrayList.size() != 0) {
            for (int i = 0; i < MainActivity.placeArrayList.size(); i++) {


                Log.e("hello", String.valueOf(Float.parseFloat(MainActivity.latitudeArrayList.get(i))));

                MainActivity.destination.setLatitude(Double.parseDouble(MainActivity.latitudeArrayList.get(i)));
                MainActivity.destination.setLongitude(Double.parseDouble(MainActivity.longitudeArrayList.get(i)));
                Log.e("distancemeters", Stri ng.valueOf(MainActivity.location.distanceTo(MainActivity.destination)));

                if (MainActivity.location.distanceTo(MainActivity.destination)<100) {




                    AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

                    audioManager.setStreamVolume(AudioManager.STREAM_RING, AudioManager.RINGER_MODE_SILENT, AudioManager.FLAG_SHOW_UI);


                } else {



                }

            }


        }
    }

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}
}
felix
  • 454
  • 9
  • 19
  • How does it not work, exactly? Are you getting an Exception? Is `onLocationChanged()` just not being called? Have you tried debugging? Adding some log prints in that method? – Mike M. Mar 30 '16 at 00:29
  • it just doesn't get called. is there a a way to put it in onStart? – felix Mar 30 '16 at 00:37
  • Oh, I misread your question the first time. I thought you meant registering didn't work. You need to call `requestLocationUpdates()` on your `GoogleApiClient` to get location update callbacks. Your `onLocationChanged()` method is in the right place. – Mike M. Mar 30 '16 at 00:45
  • How would I do that? I haven't implemented a GoogleApiClient. – felix Mar 30 '16 at 00:47
  • 1
    Sorry, I assumed you were using the `FusedLocationProviderApi`. If you're using the `LocationManager` Service, you'd call `requestLocationUpdates()` on that. – Mike M. Mar 30 '16 at 00:52
  • thanks. if you put that in an answer, I'll accept it – felix Mar 30 '16 at 01:01
  • Actually, if you don't mind, I'll just vote it as a duplicate of another question that explains it, and you should be able to mark it as helpful to close out your question. Thanks, though. Cheers! – Mike M. Mar 30 '16 at 01:07
  • all right, will do. What is the other question? – felix Mar 30 '16 at 01:08
  • Aargh! I accidentally hit the wrong vote reason. You won't be able to close it. Anyway, it was this post: http://stackoverflow.com/questions/1513485/how-do-i-get-the-current-gps-location-programmatically-in-android – Mike M. Mar 30 '16 at 01:09

0 Answers0