【问题标题】:Setting GPS position on android emulator在 android 模拟器上设置 GPS 位置
【发布时间】:2011-11-29 20:41:00
【问题描述】:

我正在尝试使用 telnet 并使用 DDMS 视角在我的模拟器上设置 GPS 位置。似乎没有任何效果,因为如果您认为我的代码屏幕上的输出将是“无法获得 GPS 位置”。当我在手机上运行该应用程序时,它可以工作。代码:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    tvLat = (TextView) findViewById(R.id.tvLat);
    tvLongi = (TextView) findViewById(R.id.tvLongi);
    tvLat.setText("test");
    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Criteria crit = new Criteria();
    providers = lm.getBestProvider(crit, false);
    Location loc = lm.getLastKnownLocation(providers);
    if (loc != null) {
        x =  loc.getLatitude();
        y =  loc.getLongitude();

        t = "Current latitude: " + x;
        t1 = "Current longitude: " + y;
        tvLat.setText(t);
        tvLongi.setText(t1);
    } else {
        tvLat.setText("Couldn't get a GPS location");
    }

}

@Override
public void onLocationChanged(Location loc) {
    // TODO Auto-generated method stub
    x = loc.getLatitude();
    y = loc.getLongitude();
    t = "Current latitude: " + x;
    t1 = "Current longitude: " + y;

    tvLat.setText(t);
    tvLongi.setText(t1);

}

@Override
public void onProviderDisabled(String arg0) {
    // TODO Auto-generated method stub
    tvLat.setText("GPS disabled");
}

@Override
public void onProviderEnabled(String arg0) {

    tvLat.setText("GPS enabled");
}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {


}
Permissions:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"></uses-permission>

【问题讨论】:

标签: android gps emulation


【解决方案1】:

从位于 Eclipse 左下角的Emulator Control 选项卡中的DDMS Perspective 中,您有Locations Controls。从那里您可以输入经度和纬度,然后单击发送将这些坐标发送到选定的模拟器。

也许您仍然无法获得最后一个已知位置,但这会触发 onLocationChanged() 方法。

编辑:我看到你的类实现了LocationListener。您是否使用lm.requestLocationUpdates() 为您的班级注册了位置更改?

EDIT2: 我的意思是:

lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,this);
lm.requestLocationUpdates(LocationManager.GPS, 0, 0,this);

【讨论】:

  • 正如我所说,我已经尝试过使用 DDMS 透视图,但我的屏幕上没有任何不同的输出。
  • “注册你的班级”是什么意思?
  • 如果我添加: lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,this);和 lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,this);屏幕上的输出将是“禁用 GPS”,如果我尝试使用 DDMS 设置位置,它会显示“无法向模拟器发送命令”
猜你喜欢
  • 2017-07-13
  • 1970-01-01
  • 2013-09-29
  • 1970-01-01
  • 2011-09-11
  • 2016-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多