GPS positioning seems to be in the room, today I got a GPS positioning small demo, including the user's longitude, latitude, height, direction, movement speed, accuracy and other information. Android provides a Locationmanager class specifically for GPS feature support, and the program does not directly create Locationmanager instances, Instead, it is obtained through the Getsystemservice () method of the context.
For example:
Create Locationmanager object Locationmanager lm = (locationmanager) getsystemservice (Context.location_service);
The following program is simple, and the layout only uses one edittext to display all the data:
Example Demo:
Mainactivity.java
Package Sn.qdj.localgpsdemo;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.widget.edittext;/** * GPS location * @author Qingdujun * */public class mainactivity Exte NDS Activity {Locationmanager lm; EditText Show; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Show = (EditText) Findviewbyid (r.id.show); Create Locationmanager Object LM = (Locationmanager) getsystemservice (Context.location_service); Obtaining the nearest location information from GPS position LC = Lm.getlastknownlocation (Locationmanager.gps_provider); Update display location information Updateview (LC); Set the GPS location information to get once every 3 seconds lm.requestlocationupdates (Locationmanager.gps_provider, 8, New Locationlistener () {@Overr Idepublicvoid Onstatuschanged (String provider, int status, Bundle extras) {//TODO auto-generated method stub} @Overridepublic void Onproviderenabled (String Provider) {//When the GPS Locationprovider is available, update the location Updateview (lm.getlastknownlocation (provider));} @Overridepublic void onproviderdisabled (String provider) {//TODO auto-generated method Stubupdateview (null);} @Overridepublic void onlocationchanged (location) {//When the GPS location information is changed, update the positioning Updateview;}}); } public void Updateview (location newlocation) {if (newlocation! = null) {StringBuilder SB = new StringBuilder () ; Sb.append ("real-time location information: \ n"); Sb.append ("Longitude: \ n"); Sb.append (Newlocation.getlongitude ()); Sb.append ("\ n Latitude:"); Sb.append ( Newlocation.getlatitude ()); Sb.append ("\ n height:"); Sb.append (Newlocation.getaltitude ()); Sb.append ("\ n speed:"); Sb.append (Newlocation.getspeed ()); Sb.append ("\ n Direction:"); Sb.append (newlocation.getbearing ()); Sb.append ("\ n positioning accuracy:"); Sb.append (Newlocation.getaccuracy ()); Show.settext (sb.tostring ());} else {show.settext (null);} }}
Activity_main.xml
<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 " tools:context= "Sn.qdj.localgpsdemo.MainActivity" > <!--display positioning information- <edittext Android : id= "@+id/show" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android: Hint= ""/></relativelayout>
GPS location needs to add a permission
<!--GPS positioning-- <uses-permission android:name= "Android.permission.ACCESS_FINE_LOCATION"/>
reference article: "Crazy Android Handout (2nd edition)" by Li Gang
Android GPS Location