我的目标是使用LocationListener中的坐标获取用户所在城市的名称,并在后台使用AsyncTask将其与Google的Maps API相匹配。
当我从MainActivity(onCreate)调用CityFinder时,什么也没发生。我没有看到来自onPreExecute的任何日志。
我需要做些什么来解决这个问题?
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import org.json.JSONException;
import org.json.JSONObject;
import static android.support.v4.app.ActivityCompat.requestPermissions;
import static com.android.volley.toolbox.Volley.*;
public class CityFinder extends AsyncTask<Void, Void, Void> {
private Activity activity;
private ProgressDialog progressDialog;
private String googleMapsGeoURL = "http://maps.googleapis.com/maps/api/geocode/json?latlng=";
private LocationManager locationManager;
private LocationListener locationListener;
protected String cityName;
public CityFinder(Activity activity, LocationManager locationManager, LocationListener locationListener) {
this.activity = activity;
this.locationManager = locationManager;
this.locationListener = locationListener;
this.progressDialog = new ProgressDialog(this.activity);
}
protected void onPreExecute() {
Log.v("CityFinder", "onPreExecute");
progressDialog.setMessage("Getting your city's name...");
progressDialog.show();
progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface arg0) {
CityFinder.this.cancel(true);
}
});
}
@Override
protected Void doInBackground(Void... params) {
try {
this.locationManager = (LocationManager) this.activity.getSystemService(this.activity.LOCATION_SERVICE);
this.locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
googleMapsGeoURL += location.getLatitude() + "," + location.getLongitude();
Log.v("CityFinder", googleMapsGeoURL);
final RequestQueue requestQueue = newRequestQueue(activity);
JsonObjectRequest request = new JsonObjectRequest(googleMapsGeoURL, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
cityName =
response.getJSONArray("results").getJSONObject(0).getString("formatted_address");
} catch (JSONException jex) {
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(request);
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
activity.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
};
locationManager.requestLocationUpdates("gps", 1000, 0, locationListener);
} catch (SecurityException se) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(this.activity, new String[]{
android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION,
android.Manifest.permission.INTERNET}, 9000);
return null;
} else {
}
Toast.makeText(activity, se.getLocalizedMessage(), Toast.LENGTH_LONG).show();
}
return null;
}
protected void onPostExecute(Void v) {
this.progressDialog.setMessage("Your city name has been found: " + this.cityName);
}
}
答案 0 :(得分:1)
创建execute
后,您错过了AsyncTask
来电。请参阅此文档以获取示例:
https://developer.android.com/reference/android/os/AsyncTask.html
由于您的doInBackground
来电并未使用params
,因此您只需使用以下命令运行:
new CityFinder(this, this.locationManager, this.locationListener).execute();