in my App I have a GpsStatus.Listener to receive events when the user enables or disables GPS. Everything works fine if GPS is on before I start the app. In
Well, LocationListener has been provided with:
onProviderDisabled(),onProviderEnabled() and onStatusChanged() for exactly this purpose.
GpsStatus.Listener delivers info about GPS service's inner workings. It is not to be used for telling the status pf GPS Provider.
LocationListener delivers info about providers. The moment you register LocationListener with location provider, onProviderEnabled()/onProviderDisabled() is called accordingly, and your app can always tell when GPS is turned on or off.
Try this:
public class test extends Activity implements LocationListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,10,this);
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
if(LocationManager.GPS_PROVIDER.equals(s)){
Toast.makeText(this,"GPS on",Toast.LENGTH_SHORT).show();
}
}
@Override
public void onProviderDisabled(String s) {
if(LocationManager.GPS_PROVIDER.equals(s)){
Toast.makeText(this,"GPS off",Toast.LENGTH_SHORT).show();
}
}
}