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?
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.