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();
答案 0 :(得分:0)
它实际上并没有停止工作,它只是花了很多时间来获取坐标..我将寻找另一种解决方案来节省时间和手机电池;从更改整个函数开始到在循环结束时设置合适的超时。