Android GPS Development (mrgps source code)

Source: Internet
Author: User

File: androidmanifest. xml

    <uses-permission android:name="android.permission.RESTART_PACKAGES" />    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>

File: Main. Java

Package COM. mrsoft. mrgps. app. mr; import android. app. activity; import android. app. activitymanager; import android. app. alertdialog; import android. app. alertdialog. builder; import android. content. context; import android. content. dialoginterface; import android. content. dialoginterface. onclicklistener; import android. location. criteria; import android. location. location; import android. location. locationlistener; impo RT android. location. locationmanager; import android. OS. bundle; import android. view. keyevent; import android. view. menu; import android. view. menuitem; import android. widget. textview; import android. widget. toast; public class main extends activity {private textview TV; private locationmanager lm; private Criteria; private location; private final static int menu_about = menu. first; private fin Al static int menu_exit = menu. first + 1;/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); TV = (textview) findviewbyid (R. id. TV); Lm = (locationmanager) getsystemservice (context. location_service); If (! Lm. isproviderenabled (Android. Location. locationmanager. gps_provider) {toast. maketext (this, "GPS is disabled. Please turn on GPS manually and try again! ", Toast. length_short ). show (); return;} else {toast. maketext (this, "GPS positioning... ", toast. length_short ). show ();} criteria = new criteria (); criteria. setaccuracy (criteria. accuracy_fine); // sets the accuracy of criteria. setaltituderequired (true); // sets the request altitude to criteria. setbearingrequired (true); // sets the request direction criteria. setcostallowed (true); // sets criteria. setpowerrequirement (criteria. power_low); // Low Power string provider = L M. getbestprovider (criteria, true); location = LM. getlastknownlocation (provider); newlocalgps (location); // listens for a 1-second ignore location change lm. requestlocationupdates (provider, 1*1000, 0, new locationlistener ();} class locationlistener implements locationlistener {@ overridepublic void onlocationchanged (location) {// todo auto-generated method stubnewlocalgps (location) ;}@ overridepublic void onproviderdisabl Ed (string provider) {// todo auto-generated method stubnewlocalgps (null) ;}@ overridepublic void onproviderenabled (string provider) {// todo auto-generated method stub} @ overridepublic void onstatuschanged (string provider, int status, bundle extras) {// todo auto-generated method stub} private void newlocalgps (location) {If (location! = NULL) {double latitude = location. getlatitude (); // double longdistance = location. getlongpolling (); // latitude double speed = location. getspeed (); // speed double altitude = location. getaltitude (); // double bearing = location. getbearing (); // orientation TV. settext ("precision" + latitude + '\ n' + "latitude" + longpolling +' \ n' + "Speed" + speed + "m/s" + '\ N' + "" + altitude + "M" + '\ n' + "" + bearing +' \ n ');} else {// location where no geographic information is obtained TV. settext ("the location of geographic information is unknown or Retrieving location of geographic information... ") ;}@ overridepublic Boolean oncreateoptionsmenu (menu) {// todo auto-generated method stub menu. add (0, menu_about, 1, "about"); menu. add (0, menu_exit, 2, "exit"); return Super. oncreateoptionsmenu (menu) ;}@ overridepublic Boolean onoptionsitemselected (menuitem item) {// todo auto-generated method stubswitch (item. getitemid () {Case menu_about: alertdialog. builder BD = new Builder (main. This); BD. setmessage ("mrgps.apk \ n version: 1.0 \ n Author: mrandexe"); BD. settitle ("about"); BD. setpositivebutton ("OK", new onclicklistener () {@ override public void onclick (dialoginterface arg0, int arg1) {// todo auto-generated method stub arg0.dismiss ();}}); BD. create (). show (); break; Case menu_exit: exit (); break;} return Super. onoptionsitemselected (item) ;}@ override public Boolean onkeydown (INT keycode, keyevent Event) {// todo auto-generated method stub if (keycode = keyevent. keycode_back & event. getrepeatcount () = 0) {exit (); Return true;} return Super. onkeydown (keycode, event);} private void exit () {alertdialog. builder = new Builder (main. this); builder. setmessage ("are you sure you want to exit? "); Builder. settitle ("prompt"); builder. setpositivebutton ("OK", new onclicklistener () {@ override public void onclick (dialoginterface arg0, int arg1) {// todo auto-generated method stub arg0.dismiss (); activitymanager actmgr = (activitymanager) getsystemservice (activity_service); actmgr. restartpackage (getpackagename () ;}}); builder. setnegativebutton ("cancel", new onclicklistener () {@ override public void onclick (dialoginterface dialog, int which) {// todo auto-generated method stub dialog. dismiss () ;}}); builder. create (). show ();}}

File: Main. xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent"  android:layout_height="fill_parent">    <TextView android:text="TextView" android:layout_width="wrap_content" android:id="@+id/tv" android:layout_height="wrap_content"></TextView></LinearLayout>

Related Article

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.