Use LocationManager to obtain complete Geographic information such as longitude and latitude.
MainActivity is as follows:
Package cc. bb; import java. util. iterator; import java. util. list; import android. location. location; import android. location. locationListener; import android. location. locationManager; import android. OS. bundle; import android. widget. textView; import android. app. activity; import android. content. context;/*** Demo Description: * LocationManager is used to implement the positioning function. * 1. Longitude is updated in real time. * 2. Geographic information is obtained based on longitude and latitude (for example, country or Street) (skipped) *** Note: * 0 is set in the test GPS It is recommended that the location be in a relatively wide space; otherwise, the positioning will be affected. * 1 use mLocationManager. getLastKnownLocation (GPSProvider) is always null when Location is obtained. * Because device location takes some time, the location logic is placed in the requestLocationUpdates () method of LocationManager ** 2 LocationManager. requestLocationUpdates * (String provider, long minTime, float minDistance, LocationListener listener) * First parameter: location information provider, for example, GPS * second parameter: Time Interval for updating location information, unit: milliseconds * third parameter: The interval between update location information. Unit: meters * Fourth parameter: callback when location information changes ** 3 LocationL OnLocationChanged () * The Most Important callback method in istener is called when both minTime and minDistance are met. document Description: * The minDistance parameter can also be used to control the * frequency of location updates. if it is greater than 0 then the * location provider will only send your application an update when * the location has changed by at least minDistance meters, AND * at least minTime milliseconds have passed. * For example, the interval (minTime) is 3 seconds and If the movement distance (minDistance) is greater than 5 meters *, this method is called. ** 4. Cancel updating the geographical location when onDestroy () of the Activity. * ** permission configuration: * <uses-permission android: name = "android. permission. ACCESS_FINE_LOCATION "/> * <uses-permission android: name =" android. permission. ACCESS_COARSE_LOCATION "/> * <uses-permission android: name =" android. permission. ACCESS_MOCK_LOCATION "/> * <uses-permission android: name =" android. permission. INTERNET "/> */public class Mai NActivity extends Activity {private Context mContext; private TextView mTextView; private LocationManager mLocationManager; private LocationListenerImpl mLocationListenerImpl; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); init (); initLocationManager (mContext);} private void init () {mContext = this; mTextView = (TextView) findViewB YId (R. id. textView);} private void initLocationManager (Context context) {mLocationManager = (LocationManager) context. getSystemService (Context. LOCATION_SERVICE); // gets available location information Provider. that is, one or more lists in passive, network, and gps <String> providerList = mLocationManager. getProviders (true); for (Iterator <String> iterator = providerList. iterator (); iterator. hasNext ();) {String provider = (String) iterator. next (); System. out. pr Intln ("provider =" + provider);} // you can use GPS to obtain the location information here. String GPSProvider = LocationManager. GPS_PROVIDER; Location location = mLocationManager. getLastKnownLocation (GPSProvider); if (location! = Null) {double longpolling = location. getlongpolling (); double altitude = location. getAltitude (); System. out. println ("longpolling =" + longpolling + ", altitude =" + altitude);} else {System. out. println ("location = null");} // register a location to listen to mLocationListenerImpl = new LocationListenerImpl (); mLocationManager. requestLocationUpdates (LocationManager. GPS_PROVIDER, 3000, 5, mLocationListenerImpl);} private class LocationListenerImpl impleme CNT LocationListener {// call this method when the device Location changes @ Overridepublic void onLocationChanged (location) {if (Location! = Null) {showLocation (location) ;}// this method is called when the provider's status changes. for example, GPS changes from available to unavailable. @ Overridepublic void onStatusChanged (String provider, int status, Bundle extras) {}// this method is called when the provider is opened instantly. for example, you can call this method when you enable GPS @ Overridepublic void onProviderEnabled (String provider) {}// when the provider is closed. for example, disable GPS @ Overridepublic void onProviderDisabled (String provider) {}} private void showLocation (Location location) {// obtain the longitude double long Usage = location. getlongpolling (); // obtain the latitude double altitude = location. getAltitude (); String message = "longitude:" + longpolling + "\ n" + "latitude:" + altitude; mTextView. setText (message) ;}@ Overrideprotected void onDestroy () {super. onDestroy (); if (mLocationManager! = Null) {mLocationManager. removeUpdates (mLocationListenerImpl );}}}
Main. xml is as follows:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/textView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_centerInParent="true" android:gravity="center" /></RelativeLayout>