0
public void getCurrentLocation(){

 final LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

     LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
         //
        }

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

        }

        public void onProviderEnabled(String provider) {}

        public void onProviderDisabled(String provider) {
            if(provider=="gps"){
            Toast.makeText(getApplicationContext(), "GPS is off", Toast.LENGTH_LONG).show();

            startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
            }
            Log.i("lm_disabled",provider);
        }
      };

  locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0, locationListener);
  locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0, locationListener);
}

This seems like a legit code. However, the if expression never runs. I have debugged my application, the provider is "gps", it is string type - but the if returns false, I guess...

What am I missing here?

WraithLux
  • 699
  • 4
  • 15
  • 22

1 Answers1

7

You need to do provider.equals("gps") in your if statement. In java, you have to compare strings using the .equals() method. In java, == checks to see if the two object references match, not if the strings are the same. For more information, see this StackoverFlow post.

Community
  • 1
  • 1
Kurtis Nusbaum
  • 30,445
  • 13
  • 78
  • 102
  • 8
    You should also use LocationManager.GPS_PROVIDER instead of "gps" since this may change in the future.. – rjam Oct 20 '11 at 14:43