Fused Location Provider

In the high day of location-based mobile deals and offers, the question of defining user’s location for specific services, collecting statistics or keeping track of a user’s movement for specific purposes remains as relevant as ever.

Since the early versions of Android, location tracking API has gained much in terms of effectiveness. There is some payback, of course: location tracking API was moved to Google Play services, so the integration with an app is painful but surely worth it.

Main benefits

As I mentioned before, location API was available when the very first versions of Android OS emerged. API calls were simple, and location tracking algorithm was fairly straightforward: you could choose one of the location providers (network or GPS) and request location updates or set up proximity alert.

But there were two main issues with this approach:

  1. In case you need to define precise location, you had to switch between network and GPS location providers (as GPS doesn’t work indoors).
  2. Proximity alerts were used to notify a user about proximity to a location, and this took its toll on the battery life.

Having worked on location implementation (tracking application, notifying users about proximity to certain locations), we’ve discovered that proximity alerts seemed to be the perfect solution. The only but was that the phone’s battery wasn’t taking it well, which inspired us to implement our own location tracking algorithm based on both GPS and network location providers (using sensors to define if a user is moving and at which speed). In fact, it was a rough sketch of the Fused Location Provider, represented in the new Android API.

Fused Location Provider analyses GPS, Cellular and Wi-Fi network location data in order to provide the highest accuracy data. Also, this provider uses different device sensors to define if a user is walking, riding a bicycle, driving a car or just standing in order to adjust the frequency of location updates.

Fused location provider can be used to get periodic location updates (Google provides us with the perfect tutorial on this, so there is no need to reinvent the wheel). It can notify us when a user is entering/leaving some area (this feature is called geofencing. More information about implementation details can be found here). What’s more, using geofencing an application can track up to 100 targets. In fact, geofencing uses cellular location data to avoid tracking distant targets (there is no point in tracking proximity to target which is 100 miles away using expensive GPS if we can use cellular location, which gives 0.7-1.5 miles’ accuracy).

A user can define one of the 3 main fused location provider modes by setting priority:

HIGH_ACCURACY, BALANCED_POWER or NO_POWER

During the Google IO presentation, a chart was presented showing effect of different priorities of the recognition algorithm as tested multiple times on Galaxy Nexus.

Priority Typical location update interval Battery drain per hour (%) Accuracy
HIGH_ACCURACY 5 seconds 7.25% ~10 meters
BALANCED_POWER 20 seconds 0.6% ~40 meters
NO_POWER N/A small ~1 mile

Activity recognition

If you remember, Fused Location Provider uses different sensors to adjust the necessary location updates frequency, for which purpose it relies on the User activity recognition service. Even though this service is used inside of the location provider, you can use it for your own purposes (fortunately, Google has provided us with  the appropriate Guide).

The Guide reads that this service can be used to define one of the 5 user states (activities):

IN_VEHICLE
ON_BICYCLE
ON_FOOT
STILL
TILTING

Of course, sometimes a user’s action cannot be defined or is beyond this scope, thus the UNKNOWN state.

Integration details

It’s been noted before that the integration of this new API requires some additional effort:

  1. First of all, in order to use this API we have to include Google services library into our project (see the guide).
  2. Secondly, we cannot be sure if the new API is available on a particular device. So we have to check. Fortunately, all we have to do is call GooglePlayServicesUtil.isGooglePlayServicesAvailable().
  3. Then we have to decide what to do if the service is unavailable (use an older version or prompt the Play Store update?).
  4. And finally, this API is available for Android 2.2 and higher, so you won’t be able to use it with the older OS versions (and some developers still support even 1.6 OS).

There is one positive thing in moving location API to Play Services: they can be updated separately from the OS update. Which supposes more frequent updates

Android 11 对于定位服务的获取机制进行了一些调整。在 Android 11 及以上版本,系统对后台应用访问位置信息施加了更严格的限制。默认情况下,对于非前台应用,Google Play 的政策要求应用必须使用 `ACCESS_FINE_LOCATION` 或 `ACCESS_COARSE_LOCATION` 权限来获取位置信息。如果你的应用是从 Android 11 或更高版本的设备上获取位置信息,而没有获取到 `NETWORK_PROVIDER` 的位置更新,可能是因为 `NETWORK_PROVIDER` 在 Android 11 中已被弃用。 如果你的应用依赖于网络位置提供者,你可以改用 Fused Location Provider API,这是 Google 推荐的方式。Fused Location Provider API 会自动选择最佳的位置源(如 GPS 和网络),以提供更准确和快速的位置信息。它还能智能地管理设备的电源使用,因为当位置信息不是必需的时候,它会减少位置更新的频率。 要使用 Fused Location Provider,你需要在应用的 `AndroidManifest.xml` 文件中添加必要的权限: ```xml <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> ``` 然后,使用 Google Play 服务的 `FusedLocationProviderClient` 来请求位置更新: ```java FusedLocationProviderClient locationClient = LocationServices.getFusedLocationProviderClient(context); locationClient.requestLocationUpdates(locationRequest, locationCallback, Looper.getMainLooper()); ``` 在这里,`locationRequest` 是你设置的位置请求参数,`locationCallback` 是位置更新的回调。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值