監聽類:
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;}@Overridepublic void onLocationChanged(Location location) {latitude = location.getLatitude();longitude = location.getLongitude();}@Overridepublic void onProviderDisabled(String provider) {Log.e("open", provider);}@Overridepublic void onProviderEnabled(String provider) {Log.e("close", provider);}@Overridepublic 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 ListenerlocationManager.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 anyfinal 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 ListenerLocationListener.getInstance(getApplicationContext()).setLocationListener();
非同步擷取:
private class GetMyLocation extends AsyncTask<Void, Void, String> {private ProgressDialog progressDialog;public GetMyLocation() {progressDialog = new ProgressDialog(EmergencyContactDetailActivity.this);progressDialog.show();}@Overrideprotected String doInBackground(Void... params) {String address = null;try {address = LocationListener.getInstance(getApplicationContext()).getAddress();} catch (Exception e) {Logging.e("GetMyLocation Error", e.getMessage());}return address;}@Overrideprotected void onPostExecute(String result) {if (progressDialog != null && progressDialog.isShowing()) {progressDialog.cancel();}textview.setText(result);}}