0
我在獲取用戶位置(我的位置)時遇到問題。我的代碼是在Android中顯示用戶的位置
double lat;
double lng;
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(context);
String provider = LocationManager.GPS_PROVIDER;
Location location = locationManager.getLastKnownLocation(provider);
if(!locationManager.isProviderEnabled(provider)){
locationManager.setTestProviderEnabled(provider, true);
}
boolean enabled = locationManager.isProviderEnabled(provider);
if(enabled){
Toast.makeText(LoginActivity.this,"provider enabled",Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(LoginActivity.this,"provider disabled",Toast.LENGTH_LONG).show();
}
if(location!=null){
lat = location.getLatitude();
lng = location.getLongitude();
AlertDialog.Builder ab=new AlertDialog.Builder(LoginActivity.this);
ab.setMessage(Html.fromHtml("<b><font color=#ff0000>Location" +"</font></b><br>"
+location.toString()+"<br>Latitude: "+lat
+"<br>Longitude "+lng));
ab.setPositiveButton("ok",null);
Toast.makeText(LoginActivity.this,"You are at "+location.toString(),Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(LoginActivity.this,"Location not found",Toast.LENGTH_LONG).show();
}
我的問題是我得到的位置作爲null
,而應用程序消息提供被啓用。我在這段代碼中沒有發現任何問題。我也在設備上測試過它,它顯示提供程序已啓用並且找不到位置。
我還沒有在類中實現位置偵聽器。 是否有必要在我的班級中實現位置監聽器?
謝謝Pich爲你的提示我在我的代碼中添加了位置列表器,它工作正常,顯示位置,但我不能在模擬器上工作。 AVD在我用戶定位清單器中關閉,但在設備上正常工作 – 2011-01-14 05:16:56