这个问题是由于原生系统设计上存在缺陷。在 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了,导致第一次开机无声。