Android GPS Development

Source: Internet
Author: User

Androidmanifest. xml

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

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;
Import 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 Final 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 enable 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.
Criteria. setaltituderequired (true); // you can specify the request altitude.
Criteria. setbearingrequired (true); // you can specify the request direction.
Criteria. setcostallowed (true); // set to allow carrier fees
Criteria. setpowerrequirement (criteria. power_low); // Low Power Consumption

String provider = LM. getbestprovider (criteria, true );
Location = LM. getlastknownlocation (provider );
Newlocalgps (location );
// Listen for the location change once every 1 second
Lm. requestlocationupdates (provider, 1*1000, 0, new locationlistener ());
}

Class locationlistener implements locationlistener
{

@ Override
Public void onlocationchanged (location ){
// Todo auto-generated method stub
Newlocalgps (location );
}

@ Override
Public void onproviderdisabled (string provider ){
// Todo auto-generated method stub
Newlocalgps (null );
}

@ Override
Public void onproviderenabled (string provider ){
// Todo auto-generated method stub

}

@ Override
Public void onstatuschanged (string provider, int status, bundle extras ){
// Todo auto-generated method stub

}

}

Private void newlocalgps (location)
{
If (location! = NULL)
{
Double latitude = location. getlatitude (); // precision
Double longpolling = location. getlongpolling (); // latitude
Double speed = location. getspeed (); // speed
Double altitude = location. getaltitude (); // altitude
Double bearing = location. getbearing (); // Azimuth
TV. settext ("precision" + latitude + '\ n' +
"Latitude" + longpolling + '\ n' +
"Speed" + speed + "m/s" + '\ n' +
"Altitude" + altitude + "M" + '\ n' +
"Orientation" + bearing + '\ n ');
}
Else
{
// Location where no geographic information is obtained
TV. settext ("the location of geographic information is unknown or the location of geographic information is being obtained ...");
}
}

@ Override
Public 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 );
}

@ Override
Public Boolean onoptionsitemselected (menuitem item ){
// Todo auto-generated method stub
Switch (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 ();

}
}

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.