2010-09-30 106 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

据说将仿真器时间更改为实时可能会影响我有。 您可以试试