我想通過單擊按鈕找到android設備的當前位置。在UIThread中運行時,地理編碼器會掛起UI,因此我必須在後臺線程上運行它。我是AsyncTasks的新手。到目前爲止,我有這一行代碼,但我幾乎不知道參數,進度,結果必須是什麼。我不想要輸入任何內容,也不需要進度,只是結果是呈現當前地址的字符串。什麼應該params,進度,這個AsyncTask的結果是
預先感謝您。
public void onClickGetCCity(View v){
new GetCurrentCity.execute();
}
private class GetCurrentCity extends AsyncTask<Void, Void, Void>{
@Override
protected void doInBackground(Void... params) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROV
IDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
if(Geocoder.isPresent()){
Geocoder gcd = new Geocoder(getApplicationContext(), Locale.getDefault());
try {
List<Address> addresses = gcd.getFromLocation(latitude, longitude, 1);
if(addresses != null) {
Address returnedAddress = addresses.get(0);
for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress = (returnedAddress.getAddressLine(i)).toString();