LocationActivity. java
/* LocationActivity. java
* @ Author octobershiner
* 2011 7 22
* SE. HIT
* A code that demonstrates locating a user's location and listening 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. widget. TextView;
Public class LocationActivity extends Activity {
/** Called when the activity is first created .*/
// Create an lcoationManager object
Private LocationManager manager;
Private static final String TAG = "location demo ";
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
// Obtain system services,
Manager = (LocationManager) getSystemService (Context. LOCATION_SERVICE );
Location location = manager. getLastKnownLocation (LocationManager. GPS_PROVIDER );
// Obtain the device location for the first time
UpdateLocation (location );
// Important function, listening for Data Testing
Manager. requestLocationUpdates (LocationManager. GPS_PROVIDER, 6000, 10,
LocationListener );
}
/* Update here to cancel GPS listening when the activity is not in the active status */
Public void onPause (){
Super. onPause ();
LocationManager. removeListener (locationListener );
}
// Create an event listener
Private final LocationListener locationListener = new LocationListener (){
Public void onLocationChanged (Location 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 ){}
};
// Obtain the user location function and use Log to display
Private void updateLocation (Location location ){
String latLng;
If (location! = Null ){
Double lat = location. getLatitude ();
Double lng = location. getlong.pdf ();
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.
View plain
<? 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" type = "codeph" text = "/codeph">
<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.
Use of LOG: http://www.bkjia.com/kf/201107/97285.html
Final Effect