LocationActivity. java
/* LocationActivity. java
* @ Author octobershiner
* 2011 7 24
* SE. HIT
* Use Criteria to select the optimal location service to demonstrate code for locating a user's location and listening for location changes
**/
Package uni. location;
Import android. app. Activity;
Import android. content. Context;
Import android. location. Criteria;
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 );
// Create a criteria object
Criteria criteria = new Criteria ();
Criteria. setAccuracy (Criteria. ACCURACY_COARSE );
// You do not need to obtain the altitude data.
Criteria. setAltitudeRequired (false );
Criteria. setBearingRequired (false );
// Set the allowable fee
Criteria. setCostAllowed (true );
// Requires low power consumption
Criteria. setPowerRequirement (Criteria. POWER_LOW );
String provider = manager. getBestProvider (criteria, false );
Log. I (TAG, "we choose" + provider );
Location location = manager. getLastKnownLocation (provider );
// Obtain the device location for the first time
UpdateLocation (location );
// Important function, listening for Data Testing
Manager. requestLocationUpdates (provider, 6000, 10,
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 );
}
}
Modify the manifest. xml file at the same time
<? 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>
Demo result:
We can see that we only require low accuracy and the lowest power. From the last line, we can see that my VM network service is not enabled, but when we select the best provider, if the parameter is set to false, you can also select the parameter.
From the column of Octobershiner