nobalG
nobalG

Reputation: 4620

location updates from an intent service(onLocationChanged is never getting called)

My question is same as this one, Android LocationLister implemented in IntentService never execute the OnLocationChanged() method but unfortunately i am not able to understand this,My code for getting the locations from the android device works fine in the activity but when it comes to the intent service ,the onLocationChanged() method is never ever getting called.

Other parts of the service are working well,as i have also implemented notification manager example in the same service to track down the values of various variables,but the variables that are modified by the onLocationChanged() are never getting modified,depicting that the method is not getting executed. Help please

Upvotes: 0

Views: 1423

Answers (2)

Sybregunne
Sybregunne

Reputation: 179

IntentService will not wait for your onLocationChangedListener and if the last part of your IntentService is to unregister your location changed listener, then there lies your problem.

What you can do is to convert your IntentService to a regular Service. Check for different actions one of which is the next step for when you receive your new location.

i.e.

private class MyLocationListener implements LocationListener {
    public void onLocationChanged(Location location) {
        Intent intent = new Intent(this,MyService.class);
        intent.setAction("LOCATION_RECEIVED");
        intent.putExtra("locationUpdate",location);
        locationManager.removeUpdates(this);
        this.startService(intent);
    }
    public void onStatusChanged(String s, int i, Bundle bundle) {}
    public void onProviderEnabled(String s) {}
    public void onProviderDisabled(String s) {}
}

and on your Service...

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent!=null) {
        String action = intent.getAction();
            if (action!= null) {
                if (action.equals("LOCATION_RECEIVED")) {
                    Location location = null;
                    if (intent.hasExtra("locationUpdate")) {
                        location = intent.getExtras().getParcelable("locationUpdate");
                        //Process new location updates here
                    }

another way is to use Pending intent on your LocationListener but it will be to the same effect as above code. A third way is to post a runnable to your IntentService from the OnLocationChangedListener (I personally haven't tried this one.)

Would appreciate if you can share some code here. And if you have further questions. I might be of help as I am working on a similar project, acquiring location from a service.

Upvotes: 2

jboi
jboi

Reputation: 11892

As far as I remember, the Location Sensor needs to run in the UI thread. An Activity runs there but a Service runs in Background.

Add some intensive logging and exception handling to your code to find out.

If this is the reason, then there was a way to create and register a Looper. Let me know and I can go and search in my code

Upvotes: 0

Related Questions