Android 位置侦听器
我正在使用location listener为我的android应用程序获取long&lat 我正在使用以下代码:Android 位置侦听器,android,listener,latitude-longitude,Android,Listener,Latitude Longitude,我正在使用location listener为我的android应用程序获取long&lat 我正在使用以下代码: public Location getLastKnownLocation() { Location location=null; try { // Get the location manager mLocMgr = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE); // List all
public Location getLastKnownLocation()
{
Location location=null;
try
{
// Get the location manager
mLocMgr = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
// List all providers:
// List<String> providers = mLocMgr.getAllProviders();
Criteria criteria = new Criteria();
bestProvider = mLocMgr.getBestProvider(criteria, false);
location = mLocMgr.getLastKnownLocation(bestProvider);
if(location==null)
{
Thread.sleep(5000);
location=mLocMgr.getLastKnownLocation(bestProvider);
}
}
catch(Exception e)
{
Log.i("program", e.getMessage());
}
return location;
}
public Location updateCurrentLocation()
{
Location mLoc=null;
try
{
mLoc=getLastKnownLocation();
mlocListener = new LocListener();
mLocMgr.requestLocationUpdates(bestProvider, 0, 0, mlocListener);
mLoc=getLastKnownLocation();
}
catch(Exception e)
{
Log.e("program", e.getMessage());
}
return mLoc;
}
公共位置getLastKnownLocation()
{
位置=空;
尝试
{
//获取位置管理器
mLocMgr=(LocationManager)ctx.getSystemService(Context.LOCATION\u服务);
//列出所有供应商:
//List providers=mLocMgr.getAllProviders();
标准=新标准();
bestProvider=mLocMgr.getBestProvider(条件,false);
位置=mLocMgr.getLastKnownLocation(最佳提供者);
if(位置==null)
{
睡眠(5000);
位置=mLocMgr.getLastKnownLocation(最佳提供者);
}
}
捕获(例外e)
{
Log.i(“程序”,例如getMessage());
}
返回位置;
}
公共位置更新当前位置()
{
位置mLoc=null;
尝试
{
mLoc=getLastKnownLocation();
mlocListener=新的LocListener();
mLocMgr.RequestLocationUpdate(bestProvider,0,0,mlocListener);
mLoc=getLastKnownLocation();
}
捕获(例外e)
{
Log.e(“程序”,e.getMessage());
}
返回mLoc;
}
它工作得非常好。但是当我们关闭设备并再次启动它时,面临着这个问题。然后我得到的位置为空。
然而,在启动谷歌地图之后,它又开始工作了。
所以你们能帮我一下吗,因为我不想在启动我的应用程序之前启动谷歌地图。感谢您的支持 您的
getLastKnownLocation
方法在关闭和打开设备电源时可能会失败
我看到,您在订阅位置更新列表器之前调用它:
mLoc=getLastKnownLocation(); //<<<<<<<<<<<<<
mlocListener = new LocListener();
mLocMgr.requestLocationUpdates(bestProvider, 0, 0, mlocListener);
然后,在
onLocationChanged(Location-Location)
中执行所需的任何更新。当您关闭和打开设备电源时,您的getLastKnownLocation
方法可能会失败
我看到,您在订阅位置更新列表器之前调用它:
mLoc=getLastKnownLocation(); //<<<<<<<<<<<<<
mlocListener = new LocListener();
mLocMgr.requestLocationUpdates(bestProvider, 0, 0, mlocListener);
然后,在
onLocationChanged(Location-Location)
Little Flyffy位置库中执行所需的任何更新:
简单,快速,容易
此库通知您的位置更新 小Flyffy位置库: 简单,快速,容易
此库通知您的位置更新 如果这解决了您的问题,那么您应该将答案标记为已接受。选票计数下方左侧的勾号符号如果这解决了您的问题,则您应将答案标记为已接受。计票下方左侧的勾号符号