Android mobile phone positioning case code and android positioning case

Source: Internet
Author: User

Android mobile phone positioning case code and android positioning case

The Code is as follows:

Package com. xuliugen. gpsdemo; import com. itheima. gpsdemo. r; import android. app. activity; import android. location. criteria; import android. location. location; import android. location. locationListener; import android. location. locationManager; import android. OS. bundle; import android. widget. textView;/*** mobile phone locator code ** @ author xuliugen **/public class MainActivity extends Activity {// use location service private LocationManager lm; private MyLocationListener listener; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); lm = (LocationManager) getSystemService (LOCATION_SERVICE); // obtain the location method. // List <String> provider = lm. getAllProviders (); // for (String l: provider) {// System. out. println (l); //} listener = new MyLocationListener (); // register the listening location service // set the condition Criteria criteria = new Criteria () for the location provider; criteria. setAccuracy (Criteria. ACCURACY_FINE); // you can specify the following parameters: // criteria. setAccuracy (Criteria. ACCURACY_FINE); // sets it to the maximum precision. // criteria. setAltitudeRequired (false); // The elevation information is not required. // criteria. setBearingRequired (false); // The orientation information is not required. // criteria. setCostAllowed (true); // whether to allow payment // criteria. setPowerRequirement (Criteria. POWER_LOW); // power requirement String proveder = lm. getBestProvider (criteria, true); lm. requestLocationUpdates (proveder, 0, 0, listener) ;}@ Override protected void onDestroy () {// TODO Auto-generated method stub super. onDestroy (); // cancel listening location service lm. removeUpdates (listener); listener = null;} class MyLocationListener implements LocationListener {/*** callback when the Location changes */public void onLocationChanged (location Location) {String longpolling = "longitude:" + location. getlongpolling (); String latitude = "latitude:" + location. getLatitude (); String accuracy = "precision:" + location. getAccuracy (); TextView textview = new TextView (MainActivity. this); textview. setText (longmargin + "\ n" + latitude + "\ n" + accuracy); setContentView (textview);}/*** enable callback when the status changes -- disable; close -- enable */public void onStatusChanged (String provider, int status, Bundle extras) {}/*** a location provider can use */public void onProviderEnabled (String provider) {}/*** a location provider cannot use */public void onProviderDisabled (String provider ){}}}

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.