Android GPS Learning

Source: Internet
Author: User
Tags dateformat

because the project to use GPS positioning, learning the next, then the learning results recorded.

To add permissions in the manifest file:
    <uses-permission android:name= "Android.permission.ACCESS_COARSE_LOCATION" ></uses-permission>    <uses-permission android:name= "Android.permission.ACCESS_FINE_LOCATION" ></uses-permission>    < Uses-permission android:name= "Android.permission.INTERNET" ></uses-permission>    <uses-permission Android:name= "Android.permission.ACCESS_NETWORK_STATE" ></uses-permission>


several objects used, Locationmanager, Locationlistener, location.

*locationmanager Location Manager, you must first create a Locationmanager object if you want to manipulate the device.

Locationmanger locationmanager= (Locationmanager) This.getsystemservice (Context.location_service);

* Locationlistener position Listener, monitor position change, monitor device switch and status. Used primarily to obtain location, in the inner class implementation:
   Class Mylisten implements Locationlistener    {        @Override public        void onlocationchanged (location location) {            //Position changed            LOG.I (TAG, "onlocationchanged ...");            if (Mhandler! = null)            {                Message msg = mhandler.obtainmessage (refresh_location, location);                Msg.sendtotarget ();            }        }        @Override public        void onproviderdisabled (String provider) {            //        }        @Override public        Void Onproviderenabled (String provider) {                    }        @Override public        void Onstatuschanged (string provider, int Status, Bundle Extras) {            //        }            }




Binding monitoring:
            Mylistengps = new Mylisten ();                        Locationmanager.requestlocationupdates (Locationmanager.gps_provider,                    check_position_interval, 0, MyListenGPS );


4 parameters in Requestlocationupdates (String provider, long mintime, float mindistance, Locationlistener listener):

String provider: Device provider, Locationmanager.gps_provider, and Locationmanager.network_provider
Long mintime: Update period of position information, in milliseconds;

Float mindistance: Position change minimum Distance: Position information is updated when the location distance changes beyond this value

Locationlistener Listener: Supervisor Listener



* location information, such as time, latitude and longitude, altitude, etc. can be obtained through location.

Location Location=locationmanager.getlastknownlocation (locationmanager.gps_provider);// Active acquisition of Locationsimpledateformat DateFormat = new SimpleDateFormat ("yyyy mm month DD Day HH:mm:ss");                Date date = new Date (Location.gettime ());                                String message = "Time:" + dateformat.format (date) + "\ n"                        + "Longitude:" + location.getlongitude () + "\ n"                        + "Latitude:" + location . Getlatitude () + "\ n";










Android GPS Learning

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.