4

App切换到后台后如何保持持续定位? - HMSCore技术团队

 2 years ago
source link: https://www.cnblogs.com/hmscore/p/16593808.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

为了保护用户隐私,大多数应用只会在前台运行时获取用户位置,当应用在后台运行时,定位功能会被禁止。这就导致APP在后台或者锁屏时无法正常记录GPS轨迹,这对打车、共享出行、跑步等需要实时记录用户轨迹的应用影响非常大,甚至影响了应用核心功能的使用体验。那对于这些应用的开发者来说,如何在用户主动授权位置信息后,让应用在后台运行时长时间保持持续定位呢?

HMS Core定位服务提供后台持续定位的能力,在获取用户主动授权的情况下可持久记录位置信息,适用于记轨迹录场景。

一、融合定位-后台定位实现方法

  1. 应用运行设备为非华为手机

  2. 使用LocationCallback开启定位之后,当应用切到后台之后,定位将会很快停止。

  3. 为了让应用切到后台之后,定位能力依旧有效,所以可以使用enableBackgroundLocation方法创建一个前台服务,用以提高应用在后台的位置更新频率。

  4. 后台定位本身不具备定位能力,后台定位需要和LocationCallback开启的定位一起使用。定位获取的数据需要在 LocationCallback对象中的onLocationResult(LocationResult locationResult) 方法中获取。

二、注意事项:

  1. 支持的设备为非华为手机

  2. 应用需要获得定位权限,且必须为“始终允许”

  3. 应用不可被设备中的省电精灵等控电应用冻结,以vivo手机为例:打开i管家-后台耗电管理-找到应用-把智能控电改成允许后台高耗电。

三、测试Demo时的注意事项:

  1. 测试时设备最好不要是充电状态,充电状态下应用可能不会被控电。

  2. 可以通过状态栏是否有 定位图表判断 设备当前是否在进行定位。以vivo手机为例:vivo手机定位开启时状态栏会展示一个定位图标,如果不开启后台定位的话应用切后台 定位图标会消失。开启后台定位能力之后,应用切后台定位图标还是存在的。

四、实现后台定位功能集成步骤

  1. 在AndroidManifest.xml中添加后台定位服务
<service
    android:name="com.huawei.location.service.BackGroundService"
    android:foregroundServiceType="location" />
  1. (可选)在Android 9及以上版本中,为保证后台定位权限的正常使用,需要在“AndroidManifest.xml”文件中配置FOREGROUND_SERVICE权限:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
  1. 创建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;
    }
}
  1. 初始化FusedLocationProviderClient对象
FusedLocationProviderClient mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
  1. 开启后台定位
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());
                }
            });
}

了解更多详情>>

访问华为开发者联盟官网
获取开发指导文档
华为移动服务开源仓库地址:GitHubGitee

关注我们,第一时间了解 HMS Core 最新技术资讯~


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK