Android gets the current location through gps
GPS Positioning includes GPS Positioning and base station positioning: GPS positioning is based on satellite positioning, requires the head to be empty, the positioning error is 2-10 meters, the Base Station Network Positioning is based on the distance from the mobile phone, the error is large, so long as the mobile phone has a signal, it can be located. The gps information can be obtained in the following steps:
1) location-related services are obtained through context.
manager =(LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
2) select the best gps or base station positioning method in the current status.
/***** @ Param manager Location Management Service * @ return best location provider // gps // wifi // */private String getProvider (LocationManager manager) {// a set of query conditions: Criteria criteria = new Criteria (); criteria. setAccuracy (Criteria. ACCURACY_FINE); // obtain the exact location and criteria. setAltitudeRequired (false); // not sensitive to altitude criteria. setPowerRequirement (Criteria. POWER_MEDIUM); // medium criteria power consumption. setSpeedRequired (true); // speed change sensitive criteria. setCostAllowed (true); // overhead communication fee // return the best location provider true, indicating that only the currently opened location device return manager is returned. getBestProvider (criteria, true );}
3) Location listener
4) register the location listener
The Code is as follows:
/*** Ensure that this class only has one instance 1 private constructor (we cannot instantiate the object of this class externally, but can only instantiate an object of the Person class inside the class) * 2 provides a static method for returning an instance. ***/public class GPSInfoProvider {LocationManager manager; // a singleton private static GPSInfoProvider mGPSInfoProvider; private static Context context; // Singleton private static MyLoactionListener listener; // 1. private GPSInfoProvider () {}; // 2. provides a static method to return one of its instances. Ensure that the Code must be executed in synchronized single-State GPSInfoProviderpublic static synchronized GPSInfoProvider getInstance (Context context) {if (mGPSInfoProvider = null) {mGPSInfoProvider = new GPSInfoProvider (); GPSInfoProvider. context = context;} return mGPSInfoProvider;} // get gps Information public String getLocation () {// get location-related services. All services are manager obtained through context = (LocationManager) context. getSystemService (Context. LOCATION_SERVICE); // obtain all positioning Methods // manager. getAllProviders (); // gps // wifi //// select the best Positioning Method in the current State String provider = getProvider (manager ); // register the location listener/*** provider Positioning method by which device is used to locate the gps agps of the Base Station Network * How long does gps take to obtain the minimum position: 1 minute * change the minimum position how many times to obtain the location again * the corresponding callback method when the location of the listener changes */manager. requestLocationUpdates (provider, 60000, 50, getListener (); SharedPreferences sp = context. getSharedPreferences ("config", Context. MODE_PRIVATE); String location = sp. getString ("location", ""); return location;} // stop gps listening public void stopGPSListener () {// The parameter is LocationListenermanager. removeUpdates (getListener ();} // returns the Listener instance private synchronized MyLoactionListener getListener () {if (listener = null) {listener = new MyLoactionListener ();} return listener ;} private class MyLoactionListener implements LocationListener {/*** method called when the mobile phone Location changes */public void onLocationChanged (location) {String latitude = "latitude" + Location. getLatitude (); // weidu String longtitude = "longtitude" + location. getlongcases (); // jingduSharedPreferences sp = context. getSharedPreferences ("config", Context. MODE_PRIVATE); Editor editor = sp. edit (); editor. putString ("location", latitude + "-" + longtitude); editor. commit (); // The last obtained location information is stored in sharedpreference}/*** call available when the status of a device changes-> unavailable-> available GPS? available */public void onStatusChanged (String provider, int status, Bundle extras) {}/*** a device is turned on by GPS. */public void onProviderEnabled (String provider) {}/** disabled GPS for a device **/public void onProviderDisabled (String provider) {}}/***** @ param manager Location Management Service * @ return best location provider // gps // wifi // */private String getProvider (LocationManager manager) {// a set of query conditions: Criteria criteria = new Criteria (); criteria. setAccuracy (Criteria. ACCURACY_FINE); // obtain the exact location and criteria. setAltitudeRequired (false); // not sensitive to altitude criteria. setPowerRequirement (Criteria. POWER_MEDIUM); // medium criteria power consumption. setSpeedRequired (true); // speed change sensitive criteria. setCostAllowed (true); // overhead communication fee // return the best location provider true, indicating that only the currently opened location device return manager is returned. getBestProvider (criteria, true );}}