為了保護用戶隱私,大多數應用只會在前臺運行時獲取用戶位置,當應用在后臺運行時,定位功能會被禁止,這就導致APP在后臺或者鎖屏時無法正常記錄GPS軌跡,這對打車、共享出行、跑步等需要實時記錄用戶軌跡的應用影響非常大,甚至影響了應用核心功能的使用體驗,那對于這些應用的開發者來說,如何在用戶主動授權位置資訊后,讓應用在后臺運行時長時間保持持續定位呢?
HMS Core定位服務提供后臺持續定位的能力,在獲取用戶主動授權的情況下可持久記錄位置資訊,適用于記軌跡錄場景,
一、融合定位-后臺定位實作方法
-
應用運行設備為非華為手機
-
使用LocationCallback開啟定位之后,當應用切到后臺之后,定位將會很快停止,
-
為了讓應用切到后臺之后,定位能力依舊有效,所以可以使用enableBackgroundLocation方法創建一個前臺服務,用以提高應用在后臺的位置更新頻率,
-
后臺定位本身不具備定位能力,后臺定位需要和LocationCallback開啟的定位一起使用,定位獲取的資料需要在 LocationCallback物件中的onLocationResult(LocationResult locationResult) 方法中獲取,
二、注意事項:
-
支持的設備為非華為手機
-
應用需要獲得定位權限,且必須為“始終允許”
-
應用不可被設備中的省電精靈等控電應用凍結,以vivo手機為例:打開i管家-后臺耗電管理-找到應用-把智能控電改成允許后臺高耗電,
三、測驗Demo時的注意事項:
-
測驗時設備最好不要是充電狀態,充電狀態下應用可能不會被控電,
-
可以通過狀態欄是否有 定位圖表判斷 設備當前是否在進行定位,以vivo手機為例:vivo手機定位開啟時狀態欄會展示一個定位圖示,如果不開啟后臺定位的話應用切后臺 定位圖示會消失,開啟后臺定位能力之后,應用切后臺定位圖示還是存在的,
四、實作后臺定位功能集成步驟
- 在AndroidManifest.xml中添加后臺定位服務
<service
android:name="com.huawei.location.service.BackGroundService"
android:foregroundServiceType="location" />
- (可選)在Android 9及以上版本中,為保證后臺定位權限的正常使用,需要在“AndroidManifest.xml”檔案中配置FOREGROUND_SERVICE權限:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
- 創建Notification物件
public class NotificationUtil {
public static final int NOTIFICATION_ID = 1;
public static Notification getNotification(Context context) {
Notification.Builder builder;
Notification notification;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
String channelId = context.getPackageName();
NotificationChannel notificationChannel =
new NotificationChannel(channelId, "LOCATION", NotificationManager.IMPORTANCE_LOW);
notificationManager.createNotificationChannel(notificationChannel);
builder = new Notification.Builder(context, channelId);
} else {
builder = new Notification.Builder(context);
}
builder.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Location SDK")
.setContentText("Running in the background ")
.setWhen(System.currentTimeMillis());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
notification = builder.build();
} else {
notification = builder.getNotification();
}
return notification;
}
}
- 初始化FusedLocationProviderClient物件
FusedLocationProviderClient mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
- 開啟后臺定位
private void enableBackgroundLocation() {
mFusedLocationProviderClient
.enableBackgroundLocation(NotificationUtil.NOTIFICATION_ID, NotificationUtil.getNotification(this))
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
LocationLog.i(TAG, "enableBackgroundLocation onSuccess");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
LocationLog.e(TAG, "enableBackgroundLocation onFailure:" + e.getMessage());
}
});
}
了解更多詳情>>
訪問華為開發者聯盟官網
獲取開發指導檔案
華為移動服務開源倉庫地址:GitHub、Gitee
關注我們,第一時間了解 HMS Core 最新技術資訊~
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/502082.html
標籤:Android
上一篇:深入分析FragmentPagerAdapter和FragmentStatePagerAdapter
下一篇:如何給玩偶建模并讓它跳個舞?
