通过网络获取所在地这个动作可能在wifi连接信号不好的时候比较慢,这个时候需要另外新启一个线程来做这件事情,同时LocationManager中的requestLocationUpdates函数需要给新起的进程发送消息,所以新起的进程必须有消息循环。
/*get the city name*/
public String getCityName(){
if (cityName != null)
return cityName;
locMan = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
LocationProvider provider = locMan.getProvider(LocationManager.NETWORK_PROVIDER);
if (provider == null)
{
Log.i("GetCity","provider is Null!/n");
}
else
{
Log.i("GetCity", provider.toString() + "/n");
}
boolean isenable = locMan.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (isenable)
{
Log.i("maindesk", "porvider is enable!/n");
}
else
{
Log.i("maindesk", "provider is unenable");
}
locMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, locationListener, Looper.myLooper());
Location mlocation = locMan.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (mlocation == null) {
mlocation = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
if (mlocation == null)
{
cityName = null;
return cityName;
}
GeoPoint gp = getGeoByLocation(mlocation);
Address mAddress = getAddressbyGeoPoint(context, gp);
cityName = mAddress.getLocality();
return cityName;
}
其中Looper.myLooper()就是获取当前线程的消息循环。