0

How can I send a notification from inside the onProviderDisabled method of a LocationListener?

So far I have been using:

Toast.makeText(mainmenu, "GPS Location providers are disabled... blah blah", Toast.LENGTH_LONG).show();}});

but that entails using a static mainmenu variable stored when the mainmenu class is first instantiated - I gather doing it that is a bad idea, in case the mainmenu object gets removed from memory I guess - so I think I should be using notifications.

I guess I need to be able to have a reference to an activity to make the intent - but what activity can I refer to since my location listener stores no such reference?

lost baby
  • 3,178
  • 4
  • 32
  • 53

3 Answers3

1

You could use your own Interface.

public interface LocationUpdatesReveiver {
    /** onLocationChanged */
    public void onLocationChanged(Location location);
}

Make your activity class implement that interface and pass a reference of it to your LocationListener class

public class MyActivity extends Activity implements LocationUpdatesReveiver{
...
        location = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        listener = new LocListener(MyActivity.this);
}

Call your activity's implemented method

public class LocListener implements LocationListener {

    private LocationUpdatesReveiver receiver;

    public LocListener(LocationUpdatesReveiver receiver) {
        this.receiver = receiver;
    }

    @Override
    public void onLocationChanged(Location location) {
        if(null != location){
            receiver.onLocationChanged(location);
        }
    }
}

This is for onLocationUpdate but you could use the same technique for pretty much everything

1

I'm not sure if this will work always, but you can call Application.getApplicationContext() and use that.

If your LocationListener is part of an Activity (in the form of an inner class) you can also use this.MyActivity.

Pedro Loureiro
  • 11,436
  • 2
  • 31
  • 37
  • using the call Application.getApplicationContext() gives this error in eclipse "getApplicationContext() Cannot make a static reference to the non-static method getApplicationContext() from the type ContextWrapper" How do I get a good reference to the application? – lost baby Jun 24 '11 at 18:42
  • In that case, you could implement the LocationListener as an inner class and use Activity.this to get your context. – Pedro Loureiro Jun 27 '11 at 13:41
  • Thats true, I opted to pass in the reference instead of using inner class since I defined my listeners inside a support class with loads of static methods and static vars, but I bet your suggestion is much better especially for android. – lost baby Jul 08 '11 at 18:55
0

You could setup your class like:

public class myClass  extends android.app.Application implements LocationListener{
    private LocationManager locationManager;
    private String provider;
    private Location location;
    ...

Then just have the function in that class:

public void onProviderDisabled(String provider) {
    Toast.makeText(getApplicationContext(), "Disenabled provider " + provider,
            Toast.LENGTH_SHORT).show();
}
Kenny
  • 5,522
  • 2
  • 18
  • 29
  • But my LocationListener is not defined inside an Activity. What would "this" refer to? – lost baby Jun 24 '11 at 18:37
  • I've edited the answer, you could extend android.app.Application then you have access to call getApplicationContext(), its a bit hacky but it works! – Kenny Jun 24 '11 at 18:45
  • well that is interesting but it seems pretty over the top for such a simple thing - seems maybe even a little dangerous to make a second application object though I don't know why. Instead of using a static reference I could pass around a reference to an activity or a reference to the application and then use app.getApplicationContext(), I guess using a reference to an application is somehow more safe that using a reference to an activity though again I'm not sure why. – lost baby Jun 24 '11 at 19:05
  • It should be ok as long as you dont override any of the methods inherited from Application, but your right passing a reference would be much neater, though i've never done it before! – Kenny Jun 24 '11 at 19:38
  • Passing a reference could be risky because that activity (or application) might be terminated. There are also risks of introducing memory leaks! http://stackoverflow.com/questions/987072/using-application-context-everywhere/987503#987503 – Pedro Loureiro Jun 27 '11 at 13:40