[Code] Android uses criteria to select a suitable geographic location service

Source: Internet
Author: User

Locationactivity. Java

/* Locationactivity. java * @ author octobershiner * 2011 7 24 * se. hit * uses criteria to select the optimal location service to demonstrate the code for locating a user's location and listening for location changes **/package uni. location; import android. app. activity; import android. content. context; import android. location. criteria; import android. location. location; import android. location. locationlistener; import android. location. locationmanager; import android. OS. bundle; import android. OS. vibrator; impor T android. util. log; import android. widget. textview; public class locationactivity extends activity {/** called when the activity is first created. * // create the lcoationmanager object private locationmanager; Private Static final string tag = "location Demo"; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); // obtain the system service, Manager = (Locationmanager) getsystemservice (context. location_service); // create a criteria object Criteria = new criteria (); criteria. setaccuracy (criteria. accuracy_coarse); // you do not need to obtain the altitude data criteria. setaltituderequired (false); criteria. setbearingrequired (false); // you can set the criteria. setcostallowed (true); // criteria requires low power consumption. setpowerrequirement (criteria. power_low); string provider = manager. getbestprovider (CRIT Eria, false); log. I (TAG, "we choose" + provider); location = manager. getlastknownlocation (provider); // obtain the device location updatelocation (location) for the first time; // an important function that listens to the data test Manager. requestlocationupdates (provider, 6000, 10, locationlistener);} // create an event listener private final locationlistener = new locationlistener () {public void onlocationchanged (location) {updatelocation (location );} Public void onproviderdisabled (string provider) {updatelocation (null); log. I (TAG, "provider now is disabled .. ");} public void onproviderenabled (string provider) {log. I (TAG, "provider now is enabled .. ");} public void onstatuschanged (string provider, int status, bundle extras) {}; // gets the user location function and displays private void updatelocation (location) using log) {string latlng; If (location! = NULL) {double lat = location. getlatitude (); double lng = location. getlongpolling (); latlng = "latitude:" + lat + "longpolling:" + LNG;} else {latlng = "can't access your location";} log. I (TAG, "the location has changed .. "); log. I (TAG, "Your location:" + latlng );}}

Modify the manifest. xml file at the same time

    <?xml version="1.0" encoding="utf-8"?>      <manifest xmlns:android="http://schemas.android.com/apk/res/android"            package="uni.location"            android:versionCode="1"            android:versionName="1.0">          <uses-sdk android:minSdkVersion="8" />                <application android:icon="@drawable/icon" android:label="@string/app_name">              <activity android:name=".LocationActivity"                        android:label="@string/app_name">                  <intent-filter>                      <action android:name="android.intent.action.MAIN" />                      <category android:name="android.intent.category.LAUNCHER" />                  </intent-filter>              </activity>                </application>              <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />       </manifest>  

Demo result:

We can see that we only require low accuracy and the lowest power. From the last line, we can see that my VM network service is not enabled, but when we select the best provider, if the parameter is set to false, you can also select the parameter.

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.