Android GPS development required materials (including test demo download)

Source: Internet
Author: User
For more information, see:
How accurate is Android GPS? Part 1: understanding location datahow accurate is Android GPS? Part 2-consuming real-time locations

Google developer docs-location strategies

Android blog-deep dive into location

GPS testing tool (Open Source)

HTML5 geolocation API-How accurate is it, really?

Test Demo project:

Gpsactivty. Java

Public class gpsactivity extends activity {private edittext; private textview logtext; private locationmanager lm; Private Static final string tag = "gpsactivity"; @ override protected void ondestroy () {// todo auto-generated method stub super. ondestroy (); lm. removeupdates (locationlistener);} private void setlog (string txt) {printtime (); setloginfo (txt);} private void setloginfo (string TX T) {logtext. settext (txt + "\ n" + "------------------ \ n" + logtext. gettext ();} private void printtime () {calendar CA = calendar. getinstance (); int year = Ca. get (calendar. year); // obtain the year int month = Ca. get (calendar. month); // obtain the month int day = Ca. get (calendar. date); // get the day int minute = Ca. get (calendar. minute); // minute int hour = Ca. get (calendar. hour); // hour int second = Ca. get (calendar. second); // second int weekofyear = Ca. get (calend AR. day_of_week); setloginfo ("current date:" + year + "year" + month + "month" + day + "day "); setloginfo (">>>" + hour + "Hour" + minute + "Minute" + second + "second") ;}@ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. gps_demo); edittext = (edittext) findviewbyid (R. id. edittext); logtext = (textview) This. findviewbyid (R. id. logtext); Lm = (locationmanager) getsystemservice (Context. location_service); // determines if GPS is enabled normally if (! Lm. isproviderenabled (locationmanager. gps_provider) {toast. maketext (this, "enable GPS navigation... ", toast. length_short ). show (); setlog ("enable GPS navigation... "); // return to enable the GPS navigation settings interface intent = new intent (settings. action_location_source_settings); startactivityforresult (intent, 0); return;} // set the query condition string bestprovider = Lm when obtaining geographical location information. getbestprovider (getcriteria (), true); // obtain location information // if no query requirement is set, the parameter of the getlastknownlocation method is Locationmanager. gps_provider location = LM. getlastknownlocation (bestprovider); updateview (location); // listener status lm. addgpsstatuslistener (listener); // bind the listener. There are four parameters // parameter 1, device: gps_provider and network_provider. // parameter 2. The Location Information Update period, unit: millisecond // parameter 3, minimum distance of location change: When the Location Distance changes beyond this value, the location information will be updated // parameter 4, listener // remarks: parameters 2 and 3, if the value of parameter 3 is not 0, parameter 3 prevails. If the value of parameter 3 is 0, the time is used for timed update. If the value of parameter 3 is 0, the system updates the value at any time. // The value is updated once per second, or the minimum displacement change is updated more than 1 meter. // Note: The update accuracy is very low here. We recommend that you start a thread in the service and run the SL Eep (10000); then execute handler. sendmessage (), update location lm. requestlocationupdates (locationmanager. gps_provider, 1000, 1, locationlistener);} // location listening private locationlistener = new locationlistener () {/*** triggered when location information changes */Public void onlocationchanged (location) {updateview (location); log. I (TAG, "Time:" + location. gettime (); log. I (TAG, "longitude:" + location. getlongpolling (); log. I (TAG, "latitude:" + location. Getlatitude (); log. I (TAG, "altitude:" + location. getaltitude (); setlog ("time:" + location. gettime (); setlog ("longitude:" + location. getlongpolling (); setlog ("latitude:" + location. getlatitude (); setlog ("altitude:" + location. getaltitude ();}/*** triggered when the GPS status changes */Public void onstatuschanged (string provider, int status, bundle extras) {Switch (Status) {// case locationprovider when the GPS status is visible. available: log. I (TAG, "current GPS status is visible"); s Etlog ("current GPS status is visible"); break; // case locationprovider when the GPS status is out of service. out_of_service: log. I (TAG, "the current GPS status is outside the service area"); setlog ("the current GPS status is outside the service area"); break; // case locationprovider when the GPS status is suspended. temporarily_unavailable: log. I (TAG, "the current GPS status is paused"); setlog ("the current GPS status is paused"); break ;}} /*** triggered when GPS is enabled */Public void onproviderenabled (string provider) {location = LM. getlastknownlocation (provider); updatev Iew (location);}/*** triggered when GPS is disabled */Public void onproviderdisabled (string provider) {updateview (null) ;}}; // status monitoring gpsstatus. listener listener = new gpsstatus. listener () {public void ongpsstatuschanged (INT event) {Switch (event) {// locate case gpsstatus for the first time. gps_event_first_fix: log. I (TAG, "first location"); setlog ("first location"); break; // satellite status change case gpsstatus. gps_event_satellite_status: log. I (TAG, "satellite status change"); SETl OG (""); // obtain the current gpsstatus = LM. getgpsstatus (null); // gets the default maximum number of satellite stars. Int maxsatellites = gpsstatus. getmaxsatellites (); // create an iterator to save all satellite iterator <gpssatellite> iters = gpsstatus. getsatellites (). iterator (); int COUNT = 0; while (iters. hasnext () & count <= maxsatellites) {gpssatellite S = iters. next (); count ++;} system. out. println ("searched:" + Count + ""); setlog ("searched:" + Count + ""); break; // Locate start case gpsstatus. gps_event_started: log. I (TAG, "locate start"); setlog ("locate start"); break; // locate end case gpsstatus. gps_event_stopped: log. I (TAG, "positioning ended"); setlog ("positioning ended"); break ;}};}; /*** update text content in real time ** @ Param location */private void updateview (location) {If (location! = NULL) {edittext. settext ("device location \ n \ N longitude:"); edittext. append (string. valueof (location. getlongdistance (); edittext. append ("\ N latitude:"); edittext. append (string. valueof (location. getlatitude ();} else {// clear edittext object edittext. geteditabletext (). clear () ;}/ ***** return query condition * @ return */private criteria getcriteria () {Criteria = new criteria (); // sets the positioning precision criteria. accuracy_coarse is rough, criteria. accuracy_fine is more precise than criteria. setaccuracy (criteria. accuracy_fine); // sets whether the speed is required for criteria. setspeedrequired (false); // sets whether to allow the carrier to charge criteria. setcostallowed (false); // sets whether the orientation information criteria is required. setbearingrequired (false); // sets whether the elevation information criteria is required. setaltituderequired (false); // sets criteria for power supply. setpowerrequirement (criteria. power_low); return criteria ;}}

Create gps_demo.xml in the layout directory.

<?xml version="1.0" encoding="utf-8"?>    <ScrollView        android:layout_width="fill_parent"        android:layout_height="fill_parent"        xmlns:android="http://schemas.android.com/apk/res/android"        ><LinearLayout     android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent">    <EditText android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:cursorVisible="false"        android:editable="false"        android:id="@+id/editText"/>   <TextView android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:minHeight="300dp"                android:id="@+id/logText"/></LinearLayout>   </ScrollView>

Add the required permissions in androidmanifest. xml.

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

Mobile phone running

Note:

1. GPS signals cannot be detected in the room. If you have obtained longitude and latitude, it is also the last GPS record cached by the mobile phone.

2. For more information about GPS Offset, see the basic knowledge about GPS Offset.

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.