This article is an example of how Android uses GPS to get a user's location and monitor location changes. Share to everyone for your reference, specific as follows:
Locationactivity.java
* * Locationactivity.java * @author Octobershiner * 7 * SE.
HIT * A demo to locate the user's location and monitor the location of the changes in the code * * * * 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;
The public class Locationactivity extends activity {/** called the ' when the ' is the ' activity ' is the ' the './/Create created Object
Private Locationmanager Manager;
private static final String TAG = "LOCATION DEMO";
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Get the service of the system, Manager = (Locationmanager) getsystemservice (Context.location_service);
Location Location = manager.getlastknownlocation (Locationmanager.gps_provider);
The first time to obtain the position of the equipment updatelocation (location); Important function, monitoring data test manager.requEstlocationupdates (Locationmanager.gps_provider, 6000, locationlistener);
/* Update here to cancel the GPS monitor when activity is not active/public void OnPause () {super.onpause ();
Locationmanager.removelistener (Locationlistener); //Create an Event listener private final Locationlistener Locationlistener = new Locationlistener () {public void Onlocationchange
D (Location Location) {updatelocation (Location);
public void onproviderdisabled (String provider) {updatelocation (null);
LOG.I (TAG, "Provider now is disabled ...");
The public void onproviderenabled (String provider) {LOG.I (TAG, "Provider now are enabled ...");
public void onstatuschanged (String provider, int status,bundle extras) {}};
Gets the function of the user location, using log to display the private void Updatelocation (Location Location) {String latlng;
if (location!= null) {Double lat = location.getlatitude ();
Double LNG = Location.getlongitude ();
LATLNG = "Latitude:" + Lat + "Longitude:" + LNG; else {latlng = ' Can ' t access your Location ";
LOG.I (TAG, "the location has changed ...");
LOG.I (TAG, "Your Location:" +LATLNG);
}
}
Only modifying the activity file is not enough, because the security of the Android system and the licensing mechanism for the service, 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 GPS positioning on the Android virtual machine debugging problem, in fact, can be simulated, we start the virtual machine, and then open the Ddms interface, the left device column will dynamically show the virtual machine on the launch of various services, the virtual machine is now unlocked after the interface, Single device column below the emulator line, then will find the following emulator control will have location control, open inside the manual tag, haha believe you have seen the latitude and longitude, you can change. Run your program and click the Send button on the latitude and longitude setting to simulate the new location.
In this demo I used the log display state, recommended the use of this method, very useful, want to know friends can refer to my another article, learn to use log skillfully, and recommend you search SUNDYZLH teaching video.
About the use of log can refer to the previous "Android programming based on log demo an activity lifecycle example detailed"
The final effect is shown in the following illustration:
I hope this article will help you with the Android program.