1. 程式人生 > >Android簡訊通知亮屏

Android簡訊通知亮屏

平臺 MTK
android版本 8.1

主動顯示功能

谷歌在android 6.0之後有一個主動顯示的功能,如果裝置支援這個功能的話,直接設定這個功能預設開啟就可以了,因為正常喚醒螢幕的操作是會影響手機功耗的.
對應的Setting屬性
(./base/core/java/android/provider/Settings.java)
在定製的時候預設開啟就可以,這種顯示,手機在收到簡訊(或其他)通知後,不會完全亮屏,谷歌自家的手機就是預設開啟該功能,設定項在"setting>display>Ambient dispaly"

        /**
         * Whether the device should doze if configured.
         * @hide
         */
         public static final String DOZE_ENABLED = "doze_enabled";

        /**
         * Whether doze should be always on.
         * @hide
         */
        public static final String DOZE_ALWAYS_ON = "doze_always_on";

        /**
         * Whether the device should pulse on pick up gesture.
         * @hide
         */
        public static final String DOZE_PULSE_ON_PICK_UP = "doze_pulse_on_pick_up";

喚醒亮屏

有的專案不支援主動顯示功能,如果還需要心亮屏,就只能完全喚醒,當顯示通知的時候,亮屏到鎖屏介面
參照MTK online上的修改,不過由於是7.0,程式碼有點差異,我們需要修改一下。

[FAQ20202] 當手機滅屏時,定製來簡訊或者微信亮屏時間
內容 (2017-07-25)
[DESCRIPTION]
手機滅屏之後,來簡訊或者資訊亮屏7s之後再黑屏。
[SOLUTION]
Android N上驗證:

這個修改為客製化的需求,可以在BaseStatusBar.java定義一個wakelock,並初始化:
import android.os.PowerManager;
private PowerManager.WakeLock mNotificationWakeLock;

mNotificationWakeLock = mPowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, TAG);
mNotificationWakeLock.setReferenceCounted(false);

方案一:
在addNotificationViews和updateNotificationViews裡面新增如下亮屏請求。當來了一條新通知或者更新一條通知的時候都會走到這兩個邏輯:
//added MTK
if (!mPowerManager.isScreenOn() && entry.key != null && entry.key.contains(“com.android.mms”)){//可以根據需要亮屏的應用,增加不同的包名判斷。
mNotificationWakeLock.acquire(5000);
Log.d(TAG, “special app notification turn screen on”);
}
//added end

android 8.1 修改一下

+    private PowerManager.WakeLock mNotificationWakeLock;

+    private void screenWakeUp(Entry entry) {
+        if(mNotificationWakeLock == null){
+            mNotificationWakeLock = mPowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, TAG);
+            mNotificationWakeLock.setReferenceCounted(false);
+        }
+        if (!mPowerManager.isScreenOn() && entry.key != null && entry.key.contains("com.google.android.apps.messaging")) {
+            mNotificationWakeLock.acquire(5000);
+            Log.d(TAG, "special app notification turn screen on");
+        }
+    }
+
     protected void addNotificationViews(Entry entry) {
         if (entry == null) {
             return;
         }
         // Add the expanded view and icon.
         mNotificationData.add(entry);
+       screenWakeUp(entry);
         updateNotifications();
     }
 
 protected void updateRowStates() {
         final int N = mStackScroller.getChildCount();
 
         int visibleNotifications = 0;
         boolean onKeyguard = mState == StatusBarState.KEYGUARD;
         int maxNotifications = -1;
         if (onKeyguard) {
             maxNotifications = getMaxKeyguardNotifications(true /* recompute */);
         }
         mStackScroller.setMaxDisplayedNotifications(maxNotifications);
         Stack<ExpandableNotificationRow> stack = new Stack<>();
         for (int i = N - 1; i >= 0; i--) {
@@ -7605,36 +7619,37 @@ public class StatusBar extends SystemUI implements DemoMode,
 
         Notification n = notification.getNotification();
         mNotificationData.updateRanking(ranking);
 
         final StatusBarNotification oldNotification = entry.notification;
         entry.notification = notification;
         mGroupManager.onEntryUpdated(entry, oldNotification);
 
         entry.updateIcons(mContext, notification);
         inflateViews(entry, mStackScroller);
 
         mForegroundServiceController.updateNotification(notification,
                 mNotificationData.getImportance(key));
 
         boolean shouldPeek = shouldPeek(entry, notification);
         boolean alertAgain = alertAgain(entry, n);
 
         updateHeadsUp(key, entry, shouldPeek, alertAgain);
+       screenWakeUp(entry);
         updateNotifications();
 
         if (!notification.isClearable()) {
             // The user may have performed a dismiss action on the notification, since it's
             // not clearable we should snap it back.
             mStackScroller.snapViewIfNeeded(entry.row);
         }
 
         if (DEBUG) {
             // Is this for you?
             boolean isForCurrentUser = isNotificationForCurrentProfiles(notification);
             Log.d(TAG, "notification is " + (isForCurrentUser ? "" : "not ") + "for you");
         }
 
         setAreThereNotifications();
     }