0
点赞
收藏
分享

微信扫一扫

android T 前台Service预研

艾米吖 2022-04-26 阅读 67

https://developer.android.com/about/versions/13/get

通知权限

在这里插入图片描述

介绍

https://developer.android.com/reference/android/Manifest.permission

  1. android T 之前的通知:默认允许通知推送,需要用户跳转至多级设置页面去设置通知权限。
  2. android T新增前台服务的通知权限(运行时权限):

使用

https://developer.android.com/about/versions/13/changes/notification-permission

  1. android 13及以上: 应用自行控制权限对话框显示时间
  2. android 12L 及以下: 通常应用启动时弹框

豁免:与媒体会话有关的通知不受此行为变更的影响。

代码

在ServiceRecord的构造方法中会回调updateFgsHasNotificationPermission方法,去异步(避免死锁)从NMS中去获取此app是否有权限发送通知;ServiceRecord仅记录,具体权限校验在NMS中。

// ServiceRecord.java
// Whether FGS package has permissions to show notifications.
boolean mFgsHasNotificationPermission;


private void updateFgsHasNotificationPermission() {
    // Do asynchronous communication with notification manager to avoid deadlocks.
    final String localPackageName = packageName;
    final int appUid = appInfo.uid;

    ams.mHandler.post(new Runnable() {
        public void run() {
            NotificationManagerInternal nm = LocalServices.getService(
                    NotificationManagerInternal.class);
            if (nm == null) {
                return;
            }
            // Record whether the package has permission to notify the user
            mFgsHasNotificationPermission = nm.areNotificationsEnabledForPackage(
                    localPackageName, appUid);
        }
    });
}

前台服务任务管理器

介绍

https://developer.android.com/about/versions/13/changes/fgs-manager
无论app的目标sdk版本是多少,android 13上都允许用户从抽屉式通知栏中停止前台服务(整个应用)。停止原因在ApplicationExitInfo表现为REASON_USER_REQUESTED。

在这里插入图片描述在这里插入图片描述

豁免

以下应用可以运行前台服务,而完全不会显示在任务管理器中:

  • 系统级应用
  • 安全应用,即具有 ROLE_EMERGENCY 角色的应用
  • 处于演示模式的设备上的应用

当以下类型的应用运行前台服务时,它们会显示在 FGS 任务管理器中,但应用名称旁边没有可以供用户按的停止按钮:

  • 设备所有者应用
  • 资料所有者应用
  • 常驻应用
  • 具有 ROLE_DIALER 角色的应用

测试

adb shell cmd activity stop-app PACKAGE_NAME

监控长期运行的前台服务

介绍

https://developer.android.com/about/versions/13/changes/battery#system-notification-long-running-fgs
如果系统检测到您的应用长时间运行某项前台服务(在 24 小时的时间段内至少运行 20 小时),便会发送通知邀请用户与前台服务 (FGS) 任务管理器互动。该通知包含以下内容:

如果前台服务的类型为 FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK 或 FOREGROUND_SERVICE_TYPE_LOCATION,系统将不会显示此通知。

代码

  1. 谷歌提交记录
commit 64ac1a923065d38fa33789b315c716f4c93312fc
Author: Jing Ji <jji@google.com>
Date:   Sat Dec 11 03:14:45 2021 -0800

    Monitor long-running foreground services
    
    If a certain package has foreground services running for a long time,
    say the accumulated durations over last X hours are more than Y hours,
    system will post a notification to remind the user.
    
    Some type of apps are subjected to be exempted, i.e. if it's already
    in the device idle allowlist. More exemption could be added in
    the follow-up CLs.
    
    Bug: 200326767
    Bug: 203105544
    Test: atest FrameworksMockingServicesTests:BackgroundRestrictionTest
    Change-Id: I3a8f34c33e7a533240abc7cf4fa569a0956eec73
  1. 主要逻辑在新增的类:
//  监控滥用(长时间运行)FGS 的跟踪器
frameworks/base/services/core/java/com/android/server/am/AppFGSTracker.java 

显示长时间运行通知

static final long DEFAULT_BG_FGS_LONG_RUNNING_THRESHOLD = 20 * ONE_HOUR;

对应的流程如下:
在这里插入图片描述

移除长时间运行通知

若FGS已经停止运行则取消该通知
在这里插入图片描述

举报

相关推荐

0 条评论