2010-09-30 156 views
1

你好我有應用程序每5秒詢問一次GPS位置,但它總是返回相同的位置。我嘗試過DDMS中的替代位置或通過telnet(地理修復......) 但不管怎樣,它都返回初始位置。 什麼錯了?Android模擬器代替GPS位置

public class App09_GPS_RepeatedAsking extends Activity { 
TextView tv1; 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    tv1 = (TextView) findViewById(R.id.textview); 

    Timer timer = new Timer(); 
    timer.scheduleAtFixedRate(new TimerTask() { 
     Handler mHandler = new Handler(); 
     @Override 
     public void run() { 
      try{ 
       mHandler.post(new Runnable(){ 
        @Override 
        public void run() { 
         LocationManager lm = (LocationManager)getSystemService(LOCATION_SERVICE); 
         Criteria criteria = new Criteria(); 
         criteria.setAccuracy(Criteria.ACCURACY_FINE); 
         criteria.setAltitudeRequired(false); 
         criteria.setBearingRequired(false); 
         criteria.setCostAllowed(true); 
         criteria.setPowerRequirement(Criteria.POWER_LOW); 

         String provider =lm.getBestProvider(criteria, false); 
         //Location loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

         Location loc = lm.getLastKnownLocation(provider); 
         String Text = "My current location is: " + "Latitud = " + loc.getLatitude() + " Longitud = " + loc.getLongitude(); 
         Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_SHORT).show();     
         Log.v("LOG",Text); 
       } 
       ); 
      }catch(Exception e){ 
       Log.v("LOG",e.toString()); 
      } 
     } 
    }, 0, 5000);  
} 
} 

回答

1

要在請求GPS位置,即使間隔,則應該使用LocationManager.requestLocationUpdates() - 方法:

http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates(java.lang.String,長整型,浮點,android.location.LocationListener)

在Android開發者網站上也有一個關於此的教程: http://developer.android.com/guide/topics/location/obtaining-user-location.html#Updates

+0

Thx for answer。我已經嘗試根據熟悉的教程製作應用程序(使用requestLocationUpdates)。但問題是一樣的。當我嘗試將GPS位置替換爲模擬器時,它只能使用一次。我試圖改變位置很多次,但位置沒有改變。我不知道什麼是錯的。 :/ – Tom 2010-10-02 18:33:22

1

據說將仿真器時間更改爲實時可能會影響我有。 您可以試試