Android 模拟器替换 GPS 位置

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

最佳答案

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

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

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

关于Android 模拟器替换 GPS 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3833989/

相关文章:

android - 自定义 HDPI 控件不可见

android - 在应用程序中覆盖库 buildConfigFields

Android 模拟器 - 三星 galaxy y

c++ - libc 是如何工作的?

android - 如何增加android虚拟设备的最大堆大小?

java - Android Studio,尝试从自定义 ListView 按钮删除 Sql 注册表时出错

android - 安卓NFC编程和ACR122U读写器

iOS 无需用户干预即可打开/关闭 GPS

algorithm - 汽车跟踪路径的卡尔曼滤波器

ios - 如何在 iOS 6 中获取 "Turn on WiFi for more Accurate User Location"