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);
}
這似乎是一個合法的代碼。但是,if表達式從不運行。我調試了我的應用程序,供應商是「gps」,它是字符串類型 - 但是如果返回false,我猜...onProviderDisabled - 區分供應商
我在這裏錯過了什麼?
謝謝!它正在工作。 – WraithLux
您還應該使用LocationManager.GPS_PROVIDER而不是「gps」,因爲這可能會在將來發生變化。 – Rick