You can obtain GPS information in androi through GPS_PROVIDER in the LOCATION_SERVICE provided by the system.
[Java]
LocationManager GpsManager = (LocationManager) this. getSystemService (Context. LOCATION_SERVICE );
Location location = GpsManager. getLastKnownLocation (LocationManager. GPS_PROVIDER );
The commonly used information in Location is:
[Java]
/*
Location. getAccuracy (); precision
Location. getAltitude (); Height: Altitude
Location. getBearing (); oriented
Location. getSpeed (); Speed
Location. getLatitude (); latitude
Location. getlongdistance (); longitude
Location. getTime (); UTC time in milliseconds
*/
GPS information mainly uses the registration callback function to set the conditions for the system to actively notify the service. The conditions are as follows:
1. Distance: when the movement distance exceeds the set value, the system will take the initiative to notify the meter
2. Time: when the time exceeds the set value, the system will take the initiative to notify you to take note of the second
[Java]
GpsManager. requestLocationUpdates (LocationManager. GPS_PROVIDER, 1000, 10, new GpsLocationListener ());
The value is set to 1 second, and 10 meters is the limit.
The following are examples:
1. Add a text to the layout file that displays the specific information (activity_wifi_example.xml)
[Html]
<LinearLayout 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: gravity = "center">
<TextView
Android: id = "@ + id/TextView_GpsInfo"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello_world"/>
</LinearLayout>
2. register the callback in the Code, set the restrictions, and refresh the GPS status information in real time (GpsExample. java)
[Java]
Package com. example. gpsexample;
Import android. app. Activity;
Import android. content. Context;
Import android. location. Location;
Import android. location. LocationListener;
Import android. location. LocationManager;
Import android. OS. Bundle;
Import android. util. Log;
Import android. view. Menu;
Import android. widget. TextView;
Public class GpsExample extends Activity {
Private final String TAG = "GpsExample ";
Private TextView mGpsInfo;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_gps_example );
MGpsInfo = (TextView) this. findViewById (R. id. TextView_GpsInfo );
LocationManager GpsManager = (LocationManager) this. getSystemService (Context. LOCATION_SERVICE );
Location location = GpsManager. getLastKnownLocation (LocationManager. GPS_PROVIDER );
PrintGpsLocation (location );
If (location = null ){
MGpsInfo. setText ("no GPS valid information ");
}
GpsManager. requestLocationUpdates (LocationManager. GPS_PROVIDER, 1000, 10, new GpsLocationListener ());
}
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
GetMenuInflater (). inflate (R. menu. activity_gps_example, menu );
Return true;
}
Public void printGpsLocation (Location location)
{
If (location! = Null)
{
/*
Location. getAccuracy (); precision
Location. getAltitude (); Height: Altitude
Location. getBearing (); oriented
Location. getSpeed (); Speed
Location. getLatitude (); latitude
Location. getlongdistance (); longitude
Location. getTime (); UTC time in milliseconds
*/
MGpsInfo. setText ("Accuracy:" + location. getAccuracy () +
"\ NAltitude:" + location. getAltitude () +
"\ NBearing:" + location. getBearing () +
"\ NSpeed:" + location. getSpeed () +
"\ NLatitude:" + location. getLatitude () +
"\ Nlongpolling:" + location. getlongpolling () +
"\ NTime:" + location. getTime ());
}
}
Public class GpsLocationListener implements LocationListener
{
Public void onLocationChanged (Location location ){
PrintGpsLocation (location );
}
Public void onProviderDisabled (String provider ){
Log. d (TAG, "ProviderDisabled:" + provider );
}
Public void onProviderEnabled (String provider ){
Log. d (TAG, "ProviderEnabled:" + provider );
}
Public void onStatusChanged (String provider, int status, Bundle extras ){
Log. d (TAG, "StatusChanged:" + provider + status );
}
}
}
3. Added support for obtaining GPS permissions in AndroidManifest. xml.
[Html]
<Uses-permission android: name = "android. permission. ACCESS_FINE_LOCATION"/>