每次用户打开应用程序时,我们都会检查我们是否获得了当前位置。如果没有,该应用程序会要求他在LocationManager
中启用位置,然后再回到应用程序。问题:有时,在某些手机中,即使在启用位置并且用户回到应用后,位置仍然是null
。所以用户被困在一个循环中。为什么位置仍然为空?我能做什么?启动应用程序时获取当前位置
String locationContext = Context.LOCATION_SERVICE;
locationManager = (LocationManager) getSystemService(locationContext);
Location location = locationManager.getLastKnownLocation(locationProvider);
if (location != null) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
final String lat = String.valueOf(latitude);
final String lon = String.valueOf(longitude);
System.out.println("Localisation: " + lat + " " + lon);
SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
String id = preferences.getString("id", null);
new sendLocation().execute(id, lat, lon);
} else {
System.out.println("NO LOCATION!!");
AlertDialog.Builder alert = new AlertDialog.Builder(Home.this);
alert.setTitle("Get started");
alert.setMessage("We need your location to detect places nearby. Please enable -Wireless Networks- in your location settings to get started.");
// Set an EditText view to get user input
final TextView input = new TextView(Home.this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
startActivity(new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
}
您是否可以在xml中启用位置服务的许可 – Riskhan 2012-08-13 12:18:33
我已经启用了权限。正如我解释的那样,获取位置信息并非总是如此 – user420574 2012-08-13 14:11:39