Locationactivity. java/* locationactivity. java * @ author octobershiner * 2011 7 22 * se. hit * a code that shows how to locate a user and listen for location changes **/package uni. location; import android. app. activity; import android. content. context; import android. location. location; import android. location. locationlistener; import android. location. locationmanager; import android. OS. bundle; import android. OS. vibrator; import android. util. log; import android Oid. 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) getsystemserv Ice (context. location_service); location = manager. getlastknownlocation (locationmanager. gps_provider); // obtain the device location updatelocation (location) for the first time; // an important function that listens to the data test Manager. requestlocationupdates (locationmanager. gps_provider, 6000, 10, locationlistener);}/* update here. When the activity is not in the active state, cancel the GPS listening */Public void onpause () {super. onpause (); locationmanager. removelistener (locationlistener);} // create an event listener priv Ate 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. Use log to display private void updatelocation (location) {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 );}}
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.
<?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>
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 your program and click the send button set in the longitude and latitude to simulate the acceptance of the new geographic location.
In this demo, I used log display status. This method is recommended for use. If you want to know more, please refer to another article of mine to learn how to use Log skillfully, I suggest you search for sundyzlh's teaching video.
Log usage
Click link
Final Effect