Android GPS Real-time update of longitude and latitude to obtain the number of satellites

Source: Internet
Author: User
Import Java. util. arraylist; import Java. util. iterator; import Java. util. list; import android. app. activity; import android. content. context; import android. content. intent; import android. location. gpssatellite; import android. location. gpsstatus; import android. location. location; import android. location. locationlistener; import android. location. locationmanager; import android. OS. bundle; import android. provider. set Permission; import android. view. keyevent; import android. view. view; import android. widget. button; import android. widget. textview; import android. widget. toast;/*** @ author liuzhidong * @ create date 2012-08 * @ modifydate 2012.09.24 */public class gpsactivity extends activity {private textview TV _gps, TV _satellites; private button bt_quit; locationmanager; private stringbuilder Sb; @ overrideprotec Ted void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_gps); TV _satellites = (textview) This. findviewbyid (R. id. TV _satellites); TV _gps = (textview) This. findviewbyid (R. id. TV _gps); bt_quit = (button) This. findviewbyid (R. id. bt_quit_gps); opengpssettings (); locationmanager = (locationmanager) This. getsystemservice (context. location_se Rvice); string provider = locationmanager. gps_provider; location = locationmanager. getlastknownlocation (provider); updatemsg (location); locationlistener LL = new locationlistener () {@ overridepublic void onlocationchanged (location) {string locinfo = updatemsg (location); TV _gps.settext (null ); TV _gps.settext (locinfo);} @ overridepublic void onstatuschanged (string provider, int status, Bundle Extras) {}@ overridepublic void onproviderenabled (string provider) {}@ overridepublic void onproviderdisabled (string provider) {}}; locationmanager. requestlocationupdates (provider, 1000, 1, LL); bt_quit.setonclicklistener (new view. onclicklistener () {@ overridepublic void onclick (view v) {gpsactivity. this. finish () ;}}); locationmanager. addgpsstatuslistener (statuslistener);} private string updatemsg (Locati On LOC) {sb = NULL; sb = new stringbuilder ("Location Information: \ n"); If (loc! = NULL) {double lat = loc. getlatitude (); double lng = loc. getlongpolling (); sb. append ("latitude:" + lat + "\ N longitude:" + LNG); If (loc. hasaccuracy () {sb. append ("\ n precision:" + loc. getaccuracy ();} If (loc. hasaltitude () {sb. append ("\ n elevation:" + loc. getaltitude () + "M");} If (loc. hasbearing () {// sb. append ("\ n Direction:" + loc. getbearing ();} If (loc. hasspeed () {If (loc. getspeed () * 3.6 <5) {sb. append ("\ n speed: 0.0 km/h");} el Se {sb. append ("\ n speed:" + loc. getspeed () * 3.6 + "km/h") ;}} else {sb. append ("no location information! ");} Return sb. tostring ();} private void opengpssettings () {locationmanager ALM = (locationmanager) This. getsystemservice (context. location_service); If (ALM. isproviderenabled (Android. location. locationmanager. gps_provider) {toast. maketext (this, "GPS module normal", toast. length_short ). show (); return;} toast. maketext (this, "Turn on GPS! ", Toast. length_short ). show (); intent = new intent (settings. action_security_settings); startactivityforresult (intent, 0); // return to the get interface after the setting is complete} public Boolean onkeydown (INT keycode, keyevent event) {If (keycode = keyevent. keycode_back & event. getrepeatcount () = 0) {gpsactivity. this. finish ();} return Super. onkeydown (keycode, event);}/*** satellite status listener */private list <gpssatellite> numsatellitelist = new arraylist <gpssatellite> (); // Private Final gpsstatus of satellite signals. listener statuslistener = new gpsstatus. listener () {public void ongpsstatuschanged (INT event) {// callback when the GPS status changes, such as the number of satellites locationmanager = (locationmanager) gpsactivity. this. getsystemservice (context. location_service); gpsstatus status = locationmanager. getgpsstatus (null); // get the current status string satelliteinfo = updategpsstatus (event, status); Complete (null); TV _satellites.settext (satelliteinfo) ;}; private string updategpsstatus (INT event, gpsstatus status) {stringbuilder sb2 = new stringbuilder (""); If (status = NULL) {sb2.append ("Number of searched satellites:" + 0 );} else if (event = gpsstatus. gps_event_satellite_status) {int maxsatellites = status. getmaxsatellites (); iterator <gpssatellite> it = status. getsatellites (). iterator (); numsatellitelist. clear (); int COUNT = 0; while (it. hasnext () & count <= maxsatellites) {gpssatellite S = it. next (); numsatellitelist. add (s); count ++;} sb2.append ("Number of searched satellites:" + numsatellitelist. size () ;}return sb2.tostring ();}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.