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 ma开发者_如何学JAVAinmenu 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?
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
.
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
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();
}
精彩评论