【发布时间】:2013-09-08 16:11:47
【问题描述】:
我开发了一个 Android 应用程序,可以在谷歌地图中显示设备的当前位置。到目前为止,没有什么新鲜事,一切都在我的 ZTE Smart a7 (Android 2.3.2) 上运行良好。但是在我的新设备 LG Maximo G (Android 4.1.2) 上,这些位置没有到达。换句话说,LocationManager 始终为空。任何人都知道为什么在这个特定的设备/安卓操作系统版本上会发生这种情况?
要启动侦听器,我使用以下代码
public void startListeners() {
//Test if locatioManager is null
if( this.lm.isProviderEnabled(LocationManager.GPS_PROVIDER)){
this.lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500L, 1F,this.currentListener);
}
if( this.lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
this.lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 500L, 1F,this.currentListener);
}
if(this.lm.getLastKnownLocation(LocationManager.GPS_PROVIDER) != null) {
this.lastLocation = this.lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
if(this.lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER) != null) {
if( this.lastLocation != null){
if( this.lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER).getAccuracy() > this.lastLocation.getAccuracy() ){
this.lastLocation= this.lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
} else{
this.lastLocation = this.lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
}
}
谢谢大家。
【问题讨论】:
-
我不确定为什么您的新设备无法获取您的位置信息,但为了将来参考,LocationManager 已被弃用。您可以考虑重构您的代码以使用新的位置 API developer.android.com/google/play-services/location.html。