在某些设备上未调用onLocationChanged [英] onLocationChanged not called on some devices
问题描述
这个问题已经问了很多遍了,但是我找不到一个始终有效的解决方案.
This is a question which has been asked numerous times but I could not find a solution that always works.
我正在使用Fused位置提供程序开发应用程序.
在onConnected()
方法中,我请求位置更新,并且一旦生成位置定位并调用onLocationChanged()
,就会启动应用程序逻辑. (请参阅下面的代码).
I am developing an application using the Fused location provider.
In the onConnected()
method, I am requesting for location updates and the application logic will be initiated once a location fix is generated and onLocationChanged()
is called. (Please refer to my code below).
问题 onLocationChanged()
方法.我使用Samsung Tab 2和Samsung Galaxy Grand进行测试.此代码在Tab 2上可以正常工作,但在Grand上不起作用.通过不起作用,我的意思是locationClient
已连接,但从未调用onLocationChanged()
.
Problem onLocationChanged()
method is never called on some devices. I use a Samsung Tab 2 and a Samsung Galaxy Grand for testing. This code works perfectly fine on the Tab 2 but does not work on Grand. By does not work, I mean that locationClient
gets connected but onLocationChanged()
is never called.
之前,我使用位置管理器来获取位置,在该实现中,发生了相同的问题.因此,我尝试实现融合的位置提供程序,但仍然遇到相同的问题.
Earlier, I used the location manager for getting location and in that implementation, the same problem occurred. So, I tried implementing the fused location provider but I still get the same problem.
有人可以帮我解决这个问题吗?这里有我想念的东西吗?
Can anyone help me out with this issue? Is there something I am missing here?
public class MainActivity extends Activity implements GooglePlayServicesClient.ConnectionCallbacks, OnConnectionFailedListener, LocationListener {
LocationClient locationclient;
LocationRequest lr;
Location loc1;
static String address;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationclient = new LocationClient(this,this,this);
locationclient.connect();
}
@Override
public void onConnected(Bundle arg0) {
// TODO Auto-generated method stub
lr=LocationRequest.create();
lr.setInterval(100);
locationclient.requestLocationUpdates(lr, this);
Log.d("LocationClient","On Connected");
}
@Override
public void onDisconnected() {
// TODO Auto-generated method stub
locationclient.disconnect();
}
@Override
public void onConnectionFailed(ConnectionResult arg0) {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(Location loc) {
// TODO Auto-generated method stub
// Application Logic
Log.d("LocationClient","Last Known Location LC:" + loc.getLatitude() + "," + loc.getLongitude());
}
}
推荐答案
我在Galaxy Nexus上观察到了相同的行为,并且发现了导致onLocationChanged()
不被调用的两件事.有
I observed same behavior on Galaxy Nexus and recognized two things causing onLocationChanged()
to not being called. There are
-
在系统设置"中未启用相应的位置源.对于
PRIORITY_HIGH_ACCURACY
,必须启用 GPS卫星,对于PRIORITY_BALANCED_POWER_ACCURACY
Wi-Fi&移动网络位置必须启用.
Corresponding location source is not enabled in System Settings. For
PRIORITY_HIGH_ACCURACY
the GPS satellites must be enabled, forPRIORITY_BALANCED_POWER_ACCURACY
Wi-Fi & mobile network location must be enabled.
即使对于已启用的位置源,也没有可用的有效位置信息.例如,启用了PRIORITY_HIGH_ACCURACY
源,但没有(或效果不佳)GPS接收(例如,设备在建筑物内).或PRIORITY_BALANCED_POWER_ACCURACY
源已启用,但Wi-Fi和移动数据均已关闭.在这种情况下,无法检测到位置并且不会调用onLocationChanged()
.
Even for the enabled location sources there can be no valid location information available. For instance, PRIORITY_HIGH_ACCURACY
source is enabled, but there is no (or not good) GPS reception (e.g. device is inside a building). Or PRIORITY_BALANCED_POWER_ACCURACY
source is enabled, but both Wi-Fi and mobile data are switched off. In those cases location cannot be detected and onLocationChanged()
will not be called.
要解决第1个问题,即使在连接到Google Services API之前,我也必须检查是否已启用必需的位置信息源,如果它们已关闭,则我通知用户要求他/她允许位置信息访问.
To fix #1 I had to check whether required location sources are enabled even before connecting to Google Services API and if they are switched off, then I notified a user asked him/her to allow location access.
为解决#2,我决定使用LocationRequest.setExpirationDuration()
.我将超时设置为大约10秒,并且在调用requestLocationUpdates()
后立即发布了延迟了同样10秒的回调.如果在requestLocationUpdates()
之前调用了延迟回调,则意味着由于原因2无法检测到位置.我进行了三次重试,然后向用户显示错误消息.
To solve #2 I decided to use LocationRequest.setExpirationDuration()
. I set timeout at about 10 seconds, and immediately after calling requestLocationUpdates()
I post a callback delayed by same 10 seconds. If delayed callback is called before requestLocationUpdates()
, this means location cannot be detected due to reason #2. I do three retries and then show user an error message.
Android文档在这个地方不是很好,这就是为什么我希望这会有所帮助.
Android documentation is not great at this place, that's why I hope this helps.
这篇关于在某些设备上未调用onLocationChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!