[Based aandroid] Android uses GPS to retrieve users' geographical locations and listen for location changes

Source: Internet
Author: User
1 Locationactivity. Java
2 /* Locationactivity. Java
3 * @ Author octobershiner
4 * 2011 7 22
5 * Se. Hit
6 * One demonstration is to locate the user's location and listen for location changesCode
7 * */
8 Package Uni. location;
9
10 Import Android. App. activity;
11 Import Android. content. context;
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 Location = manager. getlastknownlocation (locationmanager. gps_provider );
32 // Obtain the device location for the first time
33 Updatelocation (location );
34 // Important functions, listening for Data Testing
35 Manager. requestlocationupdates (locationmanager. gps_provider, 6000, 10,
36 Locationlistener );
37
38 }
39 /* Update here to cancel GPS listening when the activity is not active */
40 Public Void Onpause (){
41 Super . Onpause ();
42 Locationmanager. removelistener (locationlistener );
43 }
44
45 // Create an event listener
46 Private Final Locationlistener = New Locationlistener (){
47 Public Void Onlocationchanged (location ){
48 Updatelocation (location );
49 }
50 Public Void Onproviderdisabled (string provider ){
51 Updatelocation ( Null );
52 Log. I (TAG, "provider now is disabled ..");
53 }
54 Public Void Onproviderenabled (string provider ){
55 Log. I (TAG, "provider now is enabled ..");
56 }
57 Public Void Onstatuschanged (string provider, Int Status, bundle extras ){}
58 };
59
60 // Obtain the user location function and use Log display.
61 Private Void Updatelocation (location ){
62 String latlng;
63 If (Location! = Null ){
64 Double Lat = location. getlatitude ();
65 Double Lng = location. getlong.pdf ();
66
67 Latlng = "latitude:" + lat + "longpolling:" + LNG;
68 } Else {
69 Latlng = "can't access your location ";
70 }
71 Log. I (TAG, "the location has changed ..");
72 Log. I (TAG, "Your location:" + latlng );
73 }
74
75 }

Modifying only the activity file is not enough. Because the Android system is secure and the service is authorized, You need to modify the manifest. xml file.

 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 >


Many friends may have questions, that is, the debugging problem of GPS positioning on the android virtual machine. In fact, it can be simulated. You can start the virtual machine and then open the ddms interface, the device column on the left will dynamically display the startup status of various services on the Virtual Machine. After the virtual machine is unlocked, the emulator line under the Single-host device column will be displayed, at this time, you will find that the following emulator control has a location control. Open the manual label in it. Haha, I believe you have seen the longitude and latitude, and you can change it. Run yourProgramThen, click the send button set for the longitude and latitude to simulate the acceptance of the new geographic location.

In this demo, I used the log display status. We recommend this method for ease of use. If you want to know more, please refer to my other article.ArticleLearn how to use Log skillfully and search for sundyzlh's teaching video.

 

Click on log usage

Final Effect

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.