Roh*_*hit 18 android location-client
这个问题曾被多次提出,但我找不到始终有效的解决方案.
我正在使用Fused位置提供程序开发应用程序.在该onConnected()方法中,我请求位置更新,并且一旦生成并onLocationChanged()调用位置修复,将启动应用程序逻辑.(请参阅下面的代码).
onLocationChanged()在某些设备上永远不会调用问题方法.我使用三星Tab 2和三星Galaxy Grand进行测试.此代码在Tab 2上完美运行,但不适用于Grand.通过不起作用,我的意思是locationClient连接但onLocationChanged()永远不会被调用.
之前,我使用位置管理器获取位置,在该实现中,发生了同样的问题.所以,我尝试实现融合的位置提供程序,但我仍然遇到同样的问题.
任何人都可以帮我解决这个问题吗?这里有什么我想念的吗?
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());
}
}
Run Code Online (Sandbox Code Playgroud)
我在Galaxy Nexus上观察到了相同的行为并且发现了导致onLocationChanged()不被调用的两件事.有
系统设置中未启用相应的位置源.对于PRIORITY_HIGH_ACCURACY该GPS卫星必须启用,为PRIORITY_BALANCED_POWER_ACCURACY 的Wi-Fi和移动网络位置必须启用.
即使对于启用的位置源,也可能没有有效的位置信息.例如,PRIORITY_HIGH_ACCURACY启用了源,但没有(或不好)GPS接收(例如,设备在建筑物内).或者PRIORITY_BALANCED_POWER_ACCURACY启用了源,但Wi-Fi和移动数据都已关闭.在这些情况下,无法检测到位置,onLocationChanged()也不会调用.
要修复#1,我必须在连接到Google Services API之前检查是否已启用所需的位置来源,如果已关闭,则我通知用户要求他/她允许位置访问.
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 documentation is not great at this place, that's why I hope this helps.
| 归档时间: |
|
| 查看次数: |
7399 次 |
| 最近记录: |