监听类:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
public class LocationListener implements android.location.LocationListener {
private double latitude;
private double longitude;
private String address;
private Context context;
private static LocationListener locationListener;
public LocationListener(Context context) {
this.context = context;
}
public static LocationListener getInstance(Context context) {
if (locationListener == null) {
locationListener = new LocationListener(context);
}
return locationListener;
}
@Override
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
@Override
public void onProviderDisabled(String provider) {
Log.e("open", provider);
}
@Override
public void onProviderEnabled(String provider) {
Log.e("close", provider);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
/**
* Set Location Listener
*
* @param context
*/
public void setLocationListener() {
try {
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Criteria mCriteria = new Criteria();
mCriteria.setAccuracy(Criteria.ACCURACY_FINE);
mCriteria.setAltitudeRequired(false);
mCriteria.setBearingRequired(false);
mCriteria.setPowerRequirement(Criteria.POWER_MEDIUM);
if (locationManager.getBestProvider(mCriteria, true) != null) {
locationManager.requestLocationUpdates(locationManager.getBestProvider(mCriteria, true), 1000, 0,
locationListener);
Logging.e("addLocationListener", "start listener location");
} else {
// Try NETWORK Listener
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, locationListener);
Logging.e("addLocationListener failed", "don't get location please open listener location");
}
} catch (Exception e) {
Logging.e("SetLocationListener", e.getMessage());
}
}
/**
* Get Address
* @return
*/
public String getAddress() {
String str=requestAddress();
if(str!=null&&!str.equals("")){
address=str;
}
return address;
}
/**
* Request Address
*
* @return
*/
@SuppressWarnings("finally")
public String requestAddress() {
String address = null;
// The KEY can fill in any
final String key = "TroubleCh_Android";
String url = String
.format("http://ditu.google.cn/maps/geo?output=csv&key=%s&q=%s,%s", key, latitude, longitude);
URL myURL = null;
URLConnection httpsConn = null;
try {
myURL = new URL(url);
} catch (MalformedURLException e) {
Log.e("", e.getMessage());
} finally {
try {
httpsConn = myURL.openConnection();
if (httpsConn != null) {
InputStreamReader insr = new InputStreamReader(httpsConn.getInputStream(), "UTF-8");
BufferedReader br = new BufferedReader(insr);
String data = null;
if ((data = br.readLine()) != null) {
String[] retList = data.split(",");
if (retList.length > 2 && ("200".equals(retList[0]))) {
address = retList[2];
address = address.replace("\"", "");
}
}
insr.close();
}
} catch (IOException e) {
Log.e("", e.getMessage());
} finally {
return address;
}
}
}
}
开启监听:
// Location Listener
LocationListener.getInstance(getApplicationContext()).setLocationListener();
异步获取:
private class GetMyLocation extends AsyncTask<Void, Void, String> {
private ProgressDialog progressDialog;
public GetMyLocation() {
progressDialog = new ProgressDialog(EmergencyContactDetailActivity.this);
progressDialog.show();
}
@Override
protected String doInBackground(Void... params) {
String address = null;
try {
address = LocationListener.getInstance(getApplicationContext()).getAddress();
} catch (Exception e) {
Logging.e("GetMyLocation Error", e.getMessage());
}
return address;
}
@Override
protected void onPostExecute(String result) {
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.cancel();
}
textview.setText(result);
}
}