gpt4 book ai didi

java - Android 位置 api 不工作

转载 作者:行者123 更新时间:2023-12-01 08:07:59 25 4
gpt4 key购买 nike

我正在尝试在我的 Android 应用程序上获取 GPS 坐标。

Criteria criteria = new Criteria();
criteria.setAltitudeRequired(false);
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setBearingRequired(false);
criteria.setCostAllowed(false);
criteria.setPowerRequirement(Criteria.POWER_HIGH);
LocationManager locationManager =
(LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

Intent i = new Intent(context, IntentListener.class);
i.setAction(Actions.ACTION_UPDATE_LOCATION);
PendingIntent pi = PendingIntent.getBroadcast(
context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
String provider = locationManager.getBestProvider(criteria, true);
if (provider == null) return;
locationManager.requestLocationUpdates(provider, 90 * 1000, 30, pi);

这是接收代码。

Intent service = new Intent(context, WorkerService.class);
service.setAction(Actions.ACTION_UPDATE_LOCATION);
Location location = (Location) intent.getExtras().get(
LocationManager.KEY_LOCATION_CHANGED);

if (location == null) { // <-- Always true
return;
}

service.putExtra("lat", location.getLatitude());
service.putExtra("lon", location.getLongitude());
context.startService(service);

如您所见,我无法通过调用intent.getExtras()...等来获取Location实例,返回值始终为null。我正在运行 android 4.1.2 和 telnet 客户端的模拟器上进行测试,使用地理修复经度纬度。任何人都知道出了什么问题吗?谢谢。

附注 list 包含所有必需的权限,并且在模拟器中启用了 GPS。

P.P.S。此应用程序是一个企业应用程序,作为服务运行,没有 UI,并且应该收集 GPS 坐标。

最佳答案

您随意提到您正在模拟器上进行测试。模拟器本身不会接收模拟位置;您必须通过 DDMS 接口(interface)专门发送它们。

此外,due to a bug in the emulator ,传入位置的时间偏移设置为午夜,因此您的位置条件可能与它不匹配。

我强烈建议您使用物理设备进行测试,以确认问题仍然存在。

关于java - Android 位置 api 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19858471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com