我正在为Android位置尝试此代码,但是当我运行模拟器时,它说“遗憾的是位置停止了”......任何想法? 这是MainAcivity的代码 (我遵循了本教程http://www.vogella.com/articles/AndroidLocationAPI/article.html#locationapi_gpsenable)
public class MainActivity extends Activity implements LocationListener {
private TextView latituteField;
private TextView longitudeField;
private LocationManager locationManager;
private String provider;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
latituteField = (TextView) findViewById(R.id.TextView02);
longitudeField = (TextView) findViewById(R.id.TextView04);
// Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the locatioin provider -> use
// default
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
// Initialize the location fields
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
onLocationChanged(location);
} else {
latituteField.setText("Location not available");
longitudeField.setText("Location not available");
}
}
/* Request updates at startup */
@Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(provider, 400, 1, this);
}
/* Remove the locationlistener updates when Activity is paused */
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
@Override
public void onLocationChanged(Location location) {
int lat = (int) (location.getLatitude());
int lng = (int) (location.getLongitude());
latituteField.setText(String.valueOf(lat));
longitudeField.setText(String.valueOf(lng));
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(this, "Enabled new provider " + provider,
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(this, "Disabled provider " + provider,
Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:2)
您必须手动输入模拟器的位置。有几个选项
选项1:使用DDMS
转到您的android / tools目录,然后启动DDMS工具
选择“模拟器控制选项卡”
使用经度和纬度填充位置控制字段 值
按“发送”按钮
选项2:使用telnet
geo fix<经度> <纬度和GT;并[d海拔>]
选项3:使用第三方应用
您可以尝试android-gps-emulator app来设置位置。可能还有其他类似的应用
<强>来源
答案 1 :(得分:1)
试试这段代码我可能对你有帮助..
http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/
同时设置地理位置 您可以使用&#34; DDMS&#34; Eclipse的透视图将您的地理位置发送到模拟器或连接的设备。要打开此Perspective,请选择Window→Open Perspective→Other ...→DDMS。
在“仿真器控制”部分中,您可以输入地理坐标,然后按“发送”按钮。
答案 2 :(得分:1)
您可以使用Genymotion模拟器,在虚拟设备上设置位置非常容易。