Android 第一次开机后来通知无提示音

这个问题是由于原生系统设计上存在缺陷。在 NotificationManagerService.java 中,处理通知发布时,有个函数

int buzzBeepBlinkLocked(NotificationRecord record)

此函数会检测通知是否能发出提示音或振动。

if (hasAudibleAlert && !shouldMuteNotificationLocked(record)) {
    if (!sentAccessibilityEvent) {
        sendAccessibilityEvent(notification, record.getSbn().getPackageName());
        sentAccessibilityEvent = true;
    }
    if (DBG) Slog.v(TAG, "Interrupting!");
    if (hasValidSound) {
        if (isInCall()) {
            playInCallNotification();
            beep = true;
        } else {
            // 播放通知音
            beep = playSound(record, soundUri);
        }
        if(beep) {
            mSoundNotificationKey = key;
        }
    }

出现问题时,shouldMuteNotificationLocked 判断异常,无法进入播放提示音。

    boolean shouldMuteNotificationLocked(final NotificationRecord record) {

... ...

        // muted by listener
        final String disableEffects = disableNotificationEffects(record);
        if (disableEffects != null) {
            ZenLog.traceDisableEffects(record, disableEffects);
            return true;
        }

跟踪代码,disableNotificationEffects 返回不为空,这个函数中设置了几种情况,每种不同的禁止提示音的,对应返回不同的字符串。

private String disableNotificationEffects(NotificationRecord record) {
    if (mDisableNotificationEffects) {
        return "booleanState";
    }
    if ((mListenerHints & HINT_HOST_DISABLE_EFFECTS) != 0) {
        return "listenerHints";
    }
    if (record != null && record.getAudioAttributes() != null) {
        if ((mListenerHints & HINT_HOST_DISABLE_NOTIFICATION_EFFECTS) != 0) {
            if (record.getAudioAttributes().getUsage()
                    != AudioAttributes.USAGE_NOTIFICATION_RINGTONE) {
                return "listenerNoti";
            }
        }
        if ((mListenerHints & HINT_HOST_DISABLE_CALL_EFFECTS) != 0) {
            if (record.getAudioAttributes().getUsage()
                    == AudioAttributes.USAGE_NOTIFICATION_RINGTONE) {
                return "listenerCall";
            }
        }
    }
    if (mCallState != TelephonyManager.CALL_STATE_IDLE && !mZenModeHelper.isCall(record)) {
        return "callState";
    }
    return null;
};

第一次开机无声,就是由于这里的 mDisableNotificationEffects 值不对,被禁止。这个值在NMS服务初始化时有个判断如下。

if (0 == Settings.Global.getInt(getContext().getContentResolver(),
            Settings.Global.DEVICE_PROVISIONED, 0)) {
    mDisableNotificationEffects = true;
}

DEVICE_PROVISIONED 这个属性值是开机向导里面会去设置的,但是我们这个NMS系统服务会先于开机向导启动,所以 mDisableNotificationEffects 默认就被置为true了,导致第一次开机无声。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值