2010-04-27 35 views
0

我嘗試使用下面的代碼來獲得我的HTC Magic的GPS定位:安卓GPS位置更新不起作用

public class TestGPS extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    final TextView tv1 = (TextView)findViewById(R.id.tv1); 
    final TextView tv2 = (TextView)findViewById(R.id.tv2); 
    LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() 
    { 

     public void onStatusChanged(String provider, int status, Bundle extras) 
     { 
     } 

     public void onProviderEnabled(String provider) 
     { 
     } 

     public void onProviderDisabled(String provider) 
     { 
     } 

     public void onLocationChanged(Location location) 
     { 
      tv1.setText(String.valueOf(location.getLatitude())); 
      tv2.setText(String.valueOf(location.getLongitude())); 
     } 
    }); 
} 

}

我等了5分鐘,但沒有聽衆方法被調用。 GPS圖標顯示出來,在那裏呆了一段時間,但由於某種原因,我無法修復,即使在室外明亮的陽光下。我正在使用1.6 SDK進行HTC Magic測試。

有人可以告訴我什麼是錯的代碼?謝謝。

回答

0

代碼看起來很好。確保你有ACCESS_FINE_LOCATION權限。確保你的GPS沒有壞 - 換句話說,內置的地圖應用程序知道你在哪裏?確保您的應用程序在仿真器上運行,並通過仿真器提供修補程序。

Here is a sample project使用GPS並在硬件上正常工作。

+0

權限正常,內置的地圖應用程序正常工作... – Arutha 2010-04-27 12:09:22