在Android模拟器上设置GPS位置
问题描述:
我试图在我的模拟器上用telnet和使用DDMS透视图来设置GPS位置。似乎没有什么工作,因为如果你认为我的代码在屏幕上的输出将是“无法获得GPS位置”。当我在手机上运行应用程序时,该应用程序可以工作代码:在Android模拟器上设置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>
答
从DDMS Perspective
从位于Eclipse中的左下方你有Locations Controls
的Emulator Control
标签。从那里您可以输入经度和纬度,然后单击发送将这些坐标发送到选定的模拟器。
也许你仍然无法获取上次知道的位置,但这会触发onLocationChanged()
方法。
编辑:我看到你的班级实施LocationListener
。您是否使用lm.requestLocationUpdates()
注册了班级的位置更改?
EDIT2:我的意思是:
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,this);
lm.requestLocationUpdates(LocationManager.GPS, 0, 0,this);
的
可能重复[如何在Android模拟器模拟GPS位置?(http://stackoverflow.com/questions/2279647/how-to-emulate -gps-location-in-the-android-emulator) –