【发布时间】:2014-04-04 23:37:06
【问题描述】:
getLastKnownLocation 现在返回 null 但它在一天前完全正常工作!现在它始终为空,来自不同的手机。
可能是什么问题?我是否以错误的方式实施它? locationFetch 线程不断运行 - 内部实现了一个 while 循环 - 并负责在每次更改时调用“onLocationChanged”函数。
Runnable locationFetchRun = new Runnable()
{
public void run()
{
tracker = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
Location location;
boolean enabled;
// Initialize the location fields
while( !isFinishing() )
{
Log.i("Homepage","User id value "+user_id);
tracker = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
enabled = tracker
.isProviderEnabled(LocationManager.GPS_PROVIDER);
connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
//Brick point
if(!enabled || activeNetworkInfo.isConnected()!= true)
{
continue;
}
provider = tracker.getBestProvider(criteria, true);
location = tracker.getLastKnownLocation(provider);
if (location != null)
{
gps_state = 1;
onLocationChanged(location);
}
else
gps_state = 0;
}
}
};
//Start location fetch thread
Thread locationFetch = new Thread(locationFetchRun);
locationFetch.start();
【问题讨论】:
-
尝试重新启动设备并确保在设置中启用位置。如果一切顺利,我建议使用 LocationClient 获取当前位置
-
感谢 Libin 的建议,它实际上刚刚工作,但经过一个小时左右的更新......这使得线程杀死了我手机的电池。我会搜索 locationClient 和它的用法。