介绍
与传统的Web应用程序相比,为移动设备开发应用程序为我们提供了更多基于上下文的信息的机会。 上下文相关信息的输入之一是用户的当前位置。 这篇文章介绍了Android应用程序可以获取用户当前位置的几种方法。
位置API
在Android SDK的早期版本中,您必须手动实现位置服务,该服务将基础的位置提供程序(基于GPS或基于蜂窝网络)的抽象化。 这并不理想,因为作为应用程序开发人员,您可能不关心获取用户位置的实现细节。
幸运的是,Google的Location API为使用位置数据提供了一种更好的方法。 位置API提供以下功能:
- 融合的位置提供程序,将基础位置提供程序抽象出来。
- 地理围栏。 使您的应用程序可以设置特定位置周围的地理边界,然后在用户进入或离开这些区域时接收通知。
- 活动识别。 用户是在走路还是在开车。
检查Google Play服务
使用Location API需要在设备上安装Google Play服务应用程序。 在使用API之前,最好测试一下Google Play服务是否存在。 可以使用以下代码完成此操作:
protected boolean testPlayServices() {
int checkGooglePlayServices = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity());
if (checkGooglePlayServices != ConnectionResult.SUCCESS) {
// google play services is missing!!!!
/*
* Returns status code indicating whether there was an error.
* Can be one of following in ConnectionResult: SUCCESS, SERVICE_MISSING,
* SERVICE_VERSION_UPDATE_REQUIRED, SERVICE_DISABLED, SERVICE_INVALID.
*/
GooglePlayServicesUtil.getErrorDialog(checkGooglePlayServices, getActivity(), 1122).show();
return false;
}
return true;
}
代码清单1
代码清单1显示了如何检查Google Play服务是否存在。 如果不存在Google Play服务,则会显示一个对话框,使用户可以下载并安装Google Play服务应用程序。 如果找不到服务,则此方法返回false,并且可以将其放置在需要Play服务的任何代码周围。
获取用户位置
使用Location API的主要类是LocationClient。 要做的第一件事是实例化LocationClient并传递所需的侦听器。 请参见以下代码,通常在活动中从onCreate调用此代码,如果在片段中实例化LocationClient,则通常从onActivityCreated调用。
locationClient = new LocationClient(getActivity(), this, this);
参数为:
- 上下文
- ConnectionCallbacks 。 定义onConnected()和onDisconnected()方法。
- OnConnectionFailedListener 。 定义onConnectionFailed()方法。
实例化LocationClient时,下一步是调用LocationClient的connect()方法。 这通常是在onResume方法中完成的。 在inPause方法中,LocationClient调用了connect()方法。 这样可以确保LocationClient仅在活动运行时才处于活动状态。 当应用程序在后台运行时,如果需要持续跟踪用户的位置,最好为此创建一个后台服务。
当connect()方法成功时,将调用onConnected()回调。 在这种方法中,您可以使用以下方法获取用户的最后一个已知位置:
locationClient.getLasLocation();
定期位置更新
注册定期更新位置会涉及更多工作。 首先要做的是创建一个新的LocationRequest对象。 该对象指定用于接收位置更新的服务质量。 以下代码演示了这一点:
private static LocationRequest createLocationRequest() {
final LocationRequest locationRequest = new LocationRequest();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
// The rate at which the application actively requests location updates.
locationRequest.setInterval(60 * MILLISECONDS_IN_SECOND);
// The fastest rate at which the application receives location updates, for example when another
// application has requested a location update, this application also receives that event.
locationRequest.setFastestInterval(10 * MILLISECONDS_IN_SECOND);
return locationRequest;
}
创建LocationRequest并成功完成LocationClient的connect()方法之后,将调用onConnected方法。 在此方法中,指示LocationClient使用以下代码向应用程序发送定期的位置更新:
locationClient.requestLocationUpdates(locationRequest, this);
参数为:
- locationRequest。 指定位置更新的服务质量。
- LocationListener。 定义几个回调方法,包括onLocationChanged,当有新位置可用时调用该方法。
所需的依赖项
要在您的应用程序中使用Google Play服务,您必须在build.gradle中定义正确的依赖项。 该API有两种版本:一种用于Android 2.3。 以及更高版本,以及一个Android 2.2版本。
使用SDK管理器安装所需的软件包。 对于Android 2.3,这些是:
- Google Play服务
- Google资料库
对于Android 2.2,这些是:
- 适用于Froyo的Google Play服务
- Google资料库
因此,如果您的应用程序针对Android 2.2,则必须使用Froyo库的Google Play服务。 在您的build.gradle中指定以下依赖项:
对于Android 2.3:
dependencies {
compile 'com.google.android.gms:play-services:4.0.30'
}
对于Android 2.2:
dependencies {
compile 'com.google.android.gms:play-services:3.2.65'
}
在模拟位置进行测试
要测试模拟位置,您必须执行以下操作:
- 在开发人员选项中启用模拟位置。
- 下载示例LocationProvider示例应用程序: http : //developer.android.com/training/location/location-testing.html
- 使用要测试的位置数组修改LocationUtils类。
- 在您的设备上安装LocationProvider示例应用程序。
- 启动LocationProvider示例应用程序。
- 启动您要测试其位置功能的应用程序。
一个方便的网站来获取地址的经度和纬度以进行测试,方法是: http : //www.itouchmap.com/latlong.html
结论
在移动应用程序中使用位置数据可以为用户体验增加新的维度。 本文介绍了使用Google Location API获取用户当前位置所需的步骤。
翻译自: https://www.javacodegeeks.com/2014/02/android-location-based-services-2.html