开发 Android 猎鹰SDK 开发指南 实用工具 Android8.0权限说明

Android8.0权限说明 最后更新时间: 2021年01月22日

由于猎鹰sdk在运行时需要频繁进行定位,而从Android 8.0开始系统为实现降低功耗,对后台应用获取用户位置信息频率进行了限制,每小时只允许更新几次位置信息,详细信息请参考官方说明。按照官方指引,如果要提高位置更新频率,需要后台应用提供一个前台服务通知告知。

猎鹰sdk提供了一个接口,只需调用该接口即可在轨迹上报服务启动时为您的应用创建一个前台服务通知,轨迹上报服务停止后,该通知会自动消失。这样,当您的应用切换到后台后仍然有一个前台服务通知存在,以此规避Android8.0对后台定位的限制。

下面是启用前台服务通知的具体步骤:

第一步,创建一个通知栏

您需要创建一个通知栏,下面的代码是一个简单示例,具体请您根据自己的业务进行相关修改。

private static final String CHANNEL_ID_SERVICE_RUNNING = "CHANNEL_ID_SERVICE_RUNNING";
private Notification createNotification() {
        Notification.Builder builder;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID_SERVICE_RUNNING, "app service", NotificationManager.IMPORTANCE_LOW);
            nm.createNotificationChannel(channel);
            builder = new Notification.Builder(getApplicationContext(), CHANNEL_ID_SERVICE_RUNNING);
        } else {
            builder = new Notification.Builder(getApplicationContext());
        }
        Intent nfIntent = new Intent(TrackServiceActivity.this, TrackServiceActivity.class);
        nfIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        
builder.setContentIntent(PendingIntent.getActivity(TrackServiceActivity.this, 0, nfIntent, 0))
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("猎鹰sdk运行中")
                .setContentText("猎鹰sdk运行中");
        Notification notification = builder.build();
        return notification;
}

第二步,启动猎鹰sdk前,将创建的通知栏传入配置对象 

TrackParam trackParam = new TrackParam(Constants.SERVICE_ID, terminalId);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    trackParam.setNotification(createNotification());
}
aMapTrackClient.startTrack(trackParam, onTrackLifecycleListener);


返回顶部 示例中心 常见问题 智能客服 公众号
二维码