Location information is output in the log
Packagecom.mytest;Importandroid.app.Activity;ImportAndroid.app.ProgressDialog;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.location.Criteria;ImportAndroid.location.GpsStatus;Importandroid.location.Location;ImportAndroid.location.LocationListener;ImportAndroid.location.LocationManager;ImportAndroid.location.LocationProvider;ImportAndroid.os.Bundle;Importandroid.provider.Settings;ImportAndroid.util.Log;ImportAndroid.widget.Toast; Public classLocationactivityextendsActivity {Private Static FinalString Log_tag = "Locationactivity"; PrivateLocationmanager Locationmanager; @Overrideprotected voidOnDestroy () {//TODO auto-generated Method Stub Super. OnDestroy (); Locationmanager.removeupdates (Locationlistener); } @Overrideprotected voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stub Super. OnCreate (savedinstancestate); //Get managerLocationmanager =(Locationmanager) Getsystemservice (Context.location_service); if(!locationmanager.isproviderenabled (Locationmanager.gps_provider)) {Toast.maketext ( This, "Please turn on the GPS", Toast.length_short). Show (); Intent Intent=NewIntent (settings.action_locale_settings); Startactivityforresult (Intent,0); return; } Else { //Create a query conditionCriteria =NewCriteria (); //Set positioning accuracy criteria.accuracy_coarse relatively coarse, criteria.accuracy_fine is relatively finecriteria.setaccuracy (Criteria.accuracy_fine); //set whether speed is requiredCriteria.setspeedrequired (false); //set whether carrier charges are allowedCriteria.setcostallowed (false); //Set whether azimuth information is requiredCriteria.setbearingrequired (false); //Set whether altitude information is requiredCriteria.setaltituderequired (false); //set the demand for powercriteria.setpowerrequirement (Criteria.power_low); String Bestprovider= Locationmanager.getbestprovider (Criteria,true); Location Location=locationmanager.getlastknownlocation (Bestprovider); if(Location! =NULL) { DoubleLongitude = Location.getlongitude ();//Latitude DoubleLatitude = Location.getlatitude ();//LongitudeLOG.I (Log_tag, "Longitude:" + latitude + ", Latitude:" +longitude); } locationmanager.addgpsstatuslistener (Gpslistener); //binding monitoring, with 4 parameters//parameter 1, device: There are two kinds of gps_provider and Network_provider//parameter 2, Position information update period, in milliseconds//parameter 3, Position change minimum distance: location information is updated when the location distance changes beyond this value//parameter 4, monitoring//Note: Parameters 2 and 3, if the parameter 3 is not 0, the parameter 3 is the same, the parameter 3 is 0, the time is updated periodically, the two is 0, is refreshed at any time//3 seconds Update, or minimum displacement change more than 1 meters update once;//Note: This update is very low accuracy, it is recommended to start a thread in the service, Sleep (10000) in run, and then execute Handler.sendmessage (), update the locationLocationmanager.requestlocationupdates (Locationmanager.gps_provider, 3000, 1, Locationlistener); } } PrivateLocationlistener Locationlistener =NewLocationlistener () {@Override Public voidOnstatuschanged (String provider,intstatus, Bundle extras) { Switch(status) {//when the GPS status is visible Caselocationprovider.available:log.i (Log_tag,"Current GPS status is visible"); Break; //when the GPS status is outside the service area Caselocationprovider.out_of_service:log.i (Log_tag,"Current GPS status is out-of-service status"); Break; //when the GPS status is paused service Caselocationprovider.temporarily_unavailable:log.i (Log_tag,"Current GPS status is paused service status"); Break; }} @Override Public voidonproviderenabled (String provider) {//TODO auto-generated Method Stub} @Override Public voidonproviderdisabled (String provider) {//TODO auto-generated Method Stub} @Override Public voidonlocationchanged (location location) {LOG.V (Log_tag,"GPS position changed," + "Time:" + location.gettime () + "Longitude:" + location.getlongitude () + "Latitude:" + location.getlatit Ude () + "Elevation:" +location.getaltitude ()); } }; Gpsstatus.listener Gpslistener=NewGpsstatus.listener () {@Override Public voidOngpsstatuschanged (intevent) { Switch(event) { Casegpsstatus.gps_event_first_fix:log.v (Log_tag,"First time positioning"); Break; Casegpsstatus.gps_event_satellite_status:log.v (Log_tag,"The status of satellites has changed."); Break; Casegpsstatus.gps_event_started:log.v (Log_tag,"Start positioning"); Break; Casegpsstatus.gps_event_stopped:log.v (Log_tag,"End Positioning"); Break; } } };}
Android Basics: Get a simple example of GPS positioning