我想点击一个按钮,接收当前位置,我知道我不能立即得到位置,所以这是我所做的: 单击事件:获得位置立即在Android的
public void onClick(View v)
{
ProgressDialog MyDialog = ProgressDialog.show(MainPage.this, " " , " Loading. Please wait ... ", true);
MyActionsHandler myActionsHandler = new myActionsHandler(MainPage.this);
myActionsHandler.startSearch();
MyDialog.dismiss();
Intent intent = new Intent(MainPage.this, ResultPage.class);
startActivity(intent);
}
,这是搜索的位置
public void startSearch(long timeInterval,float distanceInterval)
{
LocationManager lm = (LocationManager)_context.getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, timeInterval,
distanceInterval, this);
while(!_locationFound)
{
//wait till location is found
}
}
public void onLocationChanged(Location location)
{
if (location != null)
{
double latitude = location.getLatitude();
double longitude = location.getLongitude();
float speed = location.getSpeed();
float bearing = location.getBearing();
Log.d("LOCATION CHANGED", location.getLatitude() + "");
Log.d("LOCATION CHANGED", location.getLongitude() + "");
try
{
doTheProcess(_searchType,latitude, longitude, speed, bearing);
_locationFound = true;
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我明白,这不工作的处理程序,因为回路是在同一个线程, 所以你有什么建议做最好的解决办法?
在requestLocationUpdates的javadoc的,有“调用线程必须是一个弯针线诸如呼叫活动的主线程”。但我还没有找到任何例子,所以我不知道它是否是正确的解决方案。
还有一个问题, getLastKnownLocation()
的工作,即使我以前从来没有调用locationManager? 感谢
使用NETWORK_PROVIDER中的LocationManager .requestLocationUpdates – MKJParekh 2011-11-07 12:24:18