Mobile is the biggest feature of mobile phones and handheld devices, you can simulate the location change of the device through the eclipse's DDMS view, change the latitude and longitude, click Send, then run the program, in the application, dynamically get the device location, and then display the current location information.
Getting location information is divided into three steps:
1. Add system permissions to support access to the LBS hardware
Java code:
< uses-permission android:name= "Android.permission.ACCESS_FINE_LOCATION" ></uses-permission>
2. Locationmanager objects that get system services
Java code:
Locationmanager Loctionmanager; String contextservice=context.location_service; // through the system service, obtains the Locationmanager object loctionmanager= (Locationmanager) Getsystemservice (Contextservice);
3. Get the location provider, through the location provider, to get location information, you can specify a specific location provider, you can also provide a standard set, so that the system according to the criteria to match the most suitable location provider, location information is provided by the location.
A. Obtaining a location via the GPS location provider (specifying a specific location provider)
Java code:
String provider== loctionmanager.getlastknownlocation (provider);
B. Use a standard collection, which allows the system to automatically select the best available location provider, providing a location
Java code:
Criteria =NewCriteria (); criteria.setaccuracy (criteria.accuracy_fine);//High PrecisionCriteria.setaltituderequired (false);//elevation not requiredCriteria.setbearingrequired (false);//No azimuth requiredCriteria.setcostallowed (true);//allowed to be spentCriteria.setpowerrequirement (Criteria.power_low);//Low power consumption//The best provider to match the above criteria from the available location providersString Provider = Loctionmanager.getbestprovider (criteria,true);//get the position of the last changeLocation location = loctionmanager.getlastknownlocation (provider);
Finally, the location information is displayed in the TextView,
Change of listening position
Monitor position change, 2 seconds, distance 10 meters
Java code:
Loctionmanager.requestlocationupdates (Provider, 2000, 10, Locationlistener);//Position ListenerPrivate FinalLocationlistener Locationlistener =NewLocationlistener () {@Override Public voidOnstatuschanged (String provider,intstatus, Bundle extras) {} @Override Public voidonproviderenabled (String provider) {} @Override Public voidonproviderdisabled (String provider) {}//triggered when position changes@Override Public voidonlocationchanged (location location) {//update TextView display with the new locationupdatewithnewlocation (location);}};
How Android obtains latitude and longitude