我想點擊一個按鈕,接收當前位置,我知道我不能立即得到位置,所以這是我所做的: 單擊事件:獲得位置立即在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