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

Source: Internet
Author: User
1   /*  Locationactivity. Java
2 * @ Author octobershiner
3 * 2011 7 24
4 * Se. Hit
5 * Use criteria to select the optimal location service to demonstrate how to locate a user and listen for location changesCode
6 * */
7 Package Uni. location;
8
9 Import Android. App. activity;
10 Import Android. content. context;
11 Import Android. Location. criteria;
12 Import Android. Location. location;
13 Import Android. Location. locationlistener;
14 Import Android. Location. locationmanager;
15 Import Android. OS. Bundle;
16 Import Android. OS. vibrator;
17 Import Android. util. log;
18 Import Android. widget. textview;
19
20 Public Class Locationactivity Extends Activity {
21 /** Called when the activity is first created. */
22 // Create an lcoationmanager object
23 Private Locationmanager manager;
24 Private Static Final String tag = "location Demo ";
25 @ Override
26 Public Void Oncreate (bundle savedinstancestate ){
27 Super . Oncreate (savedinstancestate );
28 Setcontentview (R. layout. Main );
29 // Obtain system services,
30 Manager = (locationmanager) getsystemservice (context. location_service );
31 // Create a criteria object
32 Criteria = New Criteria ();
33 Criteria. setaccuracy (criteria. accuracy_coarse );
34 // You do not need to obtain the altitude data.
35 Criteria. setaltituderequired ( False );
36 Criteria. setbearingrequired ( False );
37 // Set the allowable fee
38 Criteria. setcostallowed ( True );
39 // Low Power Consumption
40 Criteria. setpowerrequirement (criteria. power_low );
41 String provider = manager. getbestprovider (criteria, False );
42 Log. I (TAG, "we choose" + provider );
43 Location = manager. getlastknownlocation (provider );
44 // Obtain the device location for the first time
45 Updatelocation (location );
46 // Important functions, listening for Data Testing
47 Manager. requestlocationupdates (provider, 6000, 10,
48 Locationlistener );
49
50 }
51
52 // Create an event listener
53 Private Final Locationlistener = New Locationlistener (){
54 Public Void Onlocationchanged (location ){
55 Updatelocation (location );
56 }
57 Public Void Onproviderdisabled (string provider ){
58 Updatelocation ( Null );
59 Log. I (TAG, "provider now is disabled ..");
60 }
61 Public Void Onproviderenabled (string provider ){
62 Log. I (TAG, "provider now is enabled ..");
63 }
64 Public Void Onstatuschanged (string provider, Int Status, bundle extras ){}
65 };
66
67 // Obtain the user location function and use Log display.
68 Private Void Updatelocation (location ){
69 String latlng;
70 If (Location! =Null ){
71 Double Lat = location. getlatitude ();
72 Double Lng = location. getlong.pdf ();
73
74 Latlng = "latitude:" + lat + "longpolling:" + LNG;
75 } Else {
76 Latlng = "can't access your location ";
77 }
78 Log. I (TAG, "the location has changed ..");
79 Log. I (TAG, "Your location:" + latlng );
80 }
81
82 }

Modify the manifest. xml file at the same time

 1   <?  XML version = "1.0" encoding = "UTF-8"  ?>     
2 < Manifest Xmlns: Android = "Http://schemas.android.com/apk/res/android"
3 Package = "Uni. Location"
4 Android: versioncode = "1"
5 Android: versionname = "1.0" >
6 < Uses-SDK Android: minsdkversion = "8" />
7
8 < Application Android: icon = "@ Drawable/icon" Android: Label = "@ String/app_name" >
9 < Activity Android: Name = ". Locationactivity"
10 Android: Label = "@ String/app_name" >
11 < Intent-Filter >
12 < Action Android: Name = "Android. Intent. Action. Main" />
13 < Category Android: Name = "Android. Intent. Category. launcher" />
14 </ Intent-Filter >
15 </ Activity >
16
17 </ Application >
18 < Uses-Permission Android: Name = "Android. Permission. access_fine_location" />
19 </ 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.