Android Emulator取代GPS位置

时间:2010-09-30 19:11:58

标签: android gps emulation

你好我有应用程序,每5秒询问一次GPS位置,但它总是返回相同的位置。我在DDMS或telnet(geo fix ......)中尝试过替代位置 但无论如何,它返回初始位置。 什么错了?

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);       
}
}

2 个答案:

答案 0 :(得分:1)

要以均匀间隔请求GPS位置,您应该使用LocationManager.requestLocationUpdates() - 方法:

http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates(java.lang.String,long,float,android.location.LocationListener)

在Android开发者网站上也有一个关于此的教程: http://developer.android.com/guide/topics/location/obtaining-user-location.html#Updates

答案 1 :(得分:1)

据说将模拟器时间更改为实时可能有效。 你可以尝试一下