LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
makeUseOfNewLocation(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
Google 的位置服务 API 是 Google Play Services APK (这里是如何设置它)的一部分。它们是基于 Android 的 API 构建的。这些 API 提供了一个“融合位置提供程序”,而不是上面提到的提供程序。此提供程序根据准确性、电池使用情况等自动选择要使用的底层提供程序。它之所以快,是因为您从不断更新它的系统范围的服务获取位置信息。你可以使用更高级的功能,如地理围栏。
public class MyActivity extends Activity implements ConnectionCallbacks, OnConnectionFailedListener {
LocationClient locationClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
locationClient = new LocationClient(this, this, this);
}
@Override
public void onConnected(Bundle bundle) {
Location location = locationClient.getLastLocation() ;
Toast.makeText(this, "Connected to Google Play Services", Toast.LENGTH_SHORT).show();
}
@Override
public void onDisconnected() {
Toast.makeText(this, "Connected from Google Play Services.", Toast.LENGTH_SHORT).show();
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
// code to handle failed connection
// this code can be found here — http://developer.android.com/training/location/retrieve-current.html
}
请注意,locationClient.requestLocationUpdates(locationRequest, this);必须位于 onConnected回调函数内部,否则您将得到 IllegalStateException,因为您将尝试请求未连接到 Google Play Services Client 的位置。
task.addOnSuccessListener(this, new OnSuccessListener<LocationSettingsResponse>() {
@Override
public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
// All location settings are satisfied. The client can initialize
// location requests here.
// ...
}
});
task.addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
if (e instanceof ResolvableApiException) {
// Location settings are not satisfied, but this can be fixed
// by showing the user a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
ResolvableApiException resolvable = (ResolvableApiException) e;
resolvable.startResolutionForResult(MainActivity.this,
REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException sendEx) {
// Ignore the error.
}
}
}
});
Google Location Services API 是 Google Play Services 的一部分,它提供了一个更强大的高级框架,可以自动处理 位置供应商、 用户移动和 定位精度定位精度。它还根据您提供的功耗参数处理位置更新调度。在大多数情况下,您将通过使用 LocationServicesAPI 获得 更好的电池性能以及更合适的准确性。
task.addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
if (e instanceof ResolvableApiException) {
// Location settings are not satisfied, but this can be fixed
// by showing the user a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
IntentSenderRequest request = new IntentSenderRequest.Builder(
e.resolution).build();
gpsRequestLauncher.launch(request);
} catch (IntentSender.SendIntentException sendEx) {
// Ignore the error.
}
}
}
});