Mobile is the biggest feature of mobile phones and handheld devices. You can use the ddms view of eclipse to simulate device location changes, change the longitude and latitude, click send, and run the program. In the application, dynamically obtain the device location and display the current location information.
There are three steps to obtain location information:
1. Add system permissions to support access to LBs hardware
<Uses-Permission Android: Name = "android. Permission. access_fine_location">
</Uses-Permission>
2. Obtain the locationmanager object of the system service.
Locationmanager loctionmanager;
String contextservice = context. location_service;
// Obtain the locationmanager object through system services
Loctionmanager = (locationmanager) getsystemservice (contextservice );
3. obtain the location provider and obtain the location information through the location provider. You can specify a specific location provider or provide a standard set, allows the system to match the most suitable location provider according to the standard. Location Information is provided by location.
A. Obtain the location through the GPS location provider (specify the specific location provider)
String provider = locationmanager. gps_provider;
Location = loctionmanager. getlastknownlocation (provider );
B. Use a standard set to enable the system to automatically select the available best location provider and provide the location
Criteria = new criteria ();
Criteria. setaccuracy (criteria. accuracy_fine); // High Precision
Criteria. setaltituderequired (false); // altitude not required
Criteria. setbearingrequired (false); // orientation not required
Criteria. setcostallowed (true); // cost allowed
Criteria. setpowerrequirement (criteria. power_low); // Low Power Consumption
// From the available locations, the best providers' matching the above standards
String provider = loctionmanager. getbestprovider (criteria, true );
// Obtain the location of the last change
Location = loctionmanager. getlastknownlocation (provider );
Finally, the location information is displayed in textview,
Changes in listening location
// Listen for location changes, once every 2 seconds, more than 10 meters away
Loctionmanager. requestlocationupdates (provider, 2000, 10, locationlistener );
// Location listener
Private Final locationlistener = new locationlistener (){
@ Override
Public void onstatuschanged (string provider, int status, bundle extras ){
}
@ Override
Public void onproviderenabled (string provider ){
}
@ Override
Public void onproviderdisabled (string provider ){
}
// Triggered when the position changes
@ Override
Public void onlocationchanged (location ){
// Update textview display with the new location
Updatewithnewlocation (location );
}
};
By changing the location longitude and latitude, the program automatically updates the location information displayed in textview.