我想用 gps 获取用户位置这是我的代码
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
t=(TextView)findViewById(R.id.textView1);
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}
public class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
double lat=location.getLatitude();
double longt= location.getLongitude();
Geocoder gc=new Geocoder(getApplicationContext(),Locale.getDefault());
try
{
List <Address> adresses=gc.getFromLocation(lat, longt, 1);
StringBuilder sb=new StringBuilder();
if(adresses.size()>0)
{
Address address=adresses.get(0);
for(int i=0;i<address.getMaxAddressLineIndex();i++)
sb.append(address.getAddressLine(i)).append("\n");
sb.append(address.getLocality()).append("\n");
sb.append(address.getPostalCode()).append("\n");
sb.append(address.getCountryName());
}
String a="Latitude:"+lat+"\nLongtitude:"+longt+"\nAddress:"+sb.toString();
t.setText(a);
}
catch(Exception e)
{
t.setText("Error.\n"+e.toString());
}
}
我授予了所有权限;
平台为 Android 2.2.3 设备为 HTC Wildfire S
代码不会引发错误,但它也不起作用
有人知道吗?