gpt4 book ai didi

android - LocationManager.requestSingleUpdate

转载 作者:搜寻专家 更新时间:2023-11-01 09:13:11 25 4
gpt4 key购买 nike

我正在关注 Android Location Deep Dive tutorial .在部分代码中,接收者是这样注册的。

IntentFilter locIntentFilter = new 
IntentFilter(SINGLE_LOCATION_UPDATE_ACTION);
context.registerReceiver(singleUpdateReceiver, locIntentFilter);

在另一部分代码中,我们发现如下;

singleUpatePI = PendingIntent.getBroadcast(context, 0, updateIntent, PendingIntent.FLAG_UPDATE_CURRENT);
locationManager.requestSingleUpdate(criteria, singleUpatePI);

想法是,当给定精度标准的位置可用时,LocationManager 将触发 SingleUpdatePI Pending Intent 。我的名为“singleUpdateReceiver”的接收器将被调用,因为我之前注册了它

它有效,但我的 SingleUpdateReceiver 的 onReceive 方法被调用了两次(!)。这会导致问题,因为我第一次注意注销它。在任何调用中都没有额外可用。

谁能解释为什么 locationManager.requestSingleUpdate(..) 会两次触发未决 Intent ?这会导致一些问题,因为我在接收器中做的第一件事就是像这样注销它

context.unregisterReceiver(singleUpdateReceiver);

在第二次调用时,接收器未注册并抛出异常。

在 Android 2.3.3 HTC Sensation 上测试。

最佳答案

我相信这是因为下面一行...

criteria.setAccuracy(Criteria.ACCURACY_LOW);

应该是……

criteria.setAccuracy(Criteria.ACCURACY_COARSE);

我没有注意到 onReceive(...) 方法在此更改后被调用了两次。希望这会有所帮助。

关于android - LocationManager.requestSingleUpdate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6700610/

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