Android 从htc wildfire s通过gps获取用户位置时遇到问题

Android 从htc wildfire s通过gps获取用户位置时遇到问题,android,gps,location,Android,Gps,Location,我想用gps获取用户位置这是我的代码 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); t=(TextView)findViewById(R.id.textView1); LocationManager mlocManager = (Locatio

我想用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());
               }
        }
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
t=(TextView)findViewById(R.id.textView1);
LocationManager mlocManager=(LocationManager)getSystemService(Context.LOCATION\u服务);
LocationListener mlocListener=新建MyLocationListener();
mlocManager.RequestLocationUpdate(LocationManager.GPS_提供程序,0,0,mlocListener);
}
公共类MyLocationListener实现LocationListener
{
@凌驾
已更改位置上的公共无效(位置){
//TODO自动生成的方法存根
双纬度=location.getLatitude();
double long=location.getLongitude();
Geocoder gc=新的地理编码器(getApplicationContext(),Locale.getDefault());
尝试
{
列表地址=gc.getFromLocation(lat,lont,1);
StringBuilder sb=新的StringBuilder();
如果(地址大小()>0)
{
地址=地址。获取(0);

对于(int i=0;i你是否用一个免费应用程序或任何东西确认你应该收到GPS定位?有时我在30分钟、60分钟、2小时内都没有得到定位,即使我以前在同一个位置得到过定位。在我所在的地方,我从来没有在室内得到定位;但是如果我在室外走动,然后再回到室内,我会得到定位

您的代码在我的HTC上执行得很好(走出去之后)。虽然这现在不会影响任何事情,但我建议更改:

LocationListener mlocListener = new MyLocationListener();
致:


当您使MyLocationListener变得更复杂时。

您能定义“不工作”吗?您等待GPS定位的时间是否足够长?是否发生了变化?它是如何工作的?t没有变化,log cat没有显示任何内容,我等待GPS 20-30分钟…设备顶部的GPS图标闪烁,显示设备可以接收GPS信号
MyLocationListener mlocListener = new MyLocationListener();