Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 源碼剖析:Notification 的 notify 更新機制

編輯:關於 Android 程式設計

在上一篇《Android 獲取第三方音樂播放器的音樂資訊》的結尾,我們提到可以透過攔截第三方音樂播放器發送的 Notification,進而取得當前正在播放的歌曲資訊。要理解這一切,就得先摸清 Notification 的更新機制。

這一過程涉及兩個關鍵 Service:

StatusBarManagerService
NotificationManagerService

這兩個服務都在 frameworks/base/services/java/com/android/server/SystemServer.java 中被啟動:

class ServerThread extends Thread {
    public void run() {
        StatusBarManagerService statusBar = null;
        NotificationManagerService notification = null;
        statusBar = new StatusBarManagerService(context, wm);
        ServiceManager.addService(Context.STATUS_BAR_SERVICE, statusBar);
        notification = new NotificationManagerService(context, statusBar, lights);
        ServiceManager.addService(Context.NOTIFICATION_SERVICE, notification);
    }
}

下面我們一步步追蹤 Notification 的 Notify 流程。

第一步:在 Activity 中建立一個 Notification 並呼叫 notify。

NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification n = new Notification(R.drawable.chat, "Hello,there!", System.currentTimeMillis());
n.flags = Notification.FLAG_AUTO_CANCEL;
Intent i = new Intent(arg0.getContext(), NotificationShow.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(arg0.getContext(), R.string.app_name, i, PendingIntent.FLAG_UPDATE_CURRENT);
n.setLatestEventInfo(arg0.getContext(), "Hello,there!", "Hello,there,I'm john.", contentIntent);
nm.notify(R.string.app_name, n);

第二步:呼叫 NotificationManager.notify,進入 notify 方法。

public void notify(int id, Notification notification) {
    notify(null, id, notification);
}
public void notify(String tag, int id, Notification notification) {
    INotificationManager service = getService();
    service.enqueueNotificationWithTag(pkg, mContext.getOpPackageName(), tag, id, notification, idOut, UserHandle.myUserId());
}

第三步:notify 方法內部會呼叫 getService(),透過 ServiceManager.getService("notification") 取得 NotificationManagerService 的 binder 物件,再回到第二步呼叫 service.enqueueNotificationWithTag。

static public INotificationManager getService() {
    if (sService != null) {
        return sService;
    }
    IBinder b = ServiceManager.getService("notification");
    sService = INotificationManager.Stub.asInterface(b);
    return sService;
}

第四步:進入 NotificationManagerService 的 enqueueNotificationWithTag,它會把傳入的 Notification 封裝成 StatusBarNotification 物件,再呼叫 mStatusBar.updateNotification 進行更新。

public void enqueueNotificationWithTag(String pkg, String basePkg, String tag, int id, Notification notification, int[] idOut, int userId) {
    enqueueNotificationInternal(pkg, basePkg, Binder.getCallingUid(), Binder.getCallingPid(), tag, id, notification, idOut, userId);
}
public void enqueueNotificationInternal(final String pkg, String basePkg, final int callingUid, final int callingPid, final String tag, final int id, final Notification notification, int[] idOut, int incomingUserId) {
    final StatusBarNotification n = new StatusBarNotification(pkg, id, tag, callingUid, callingPid, score, notification, user);
    mStatusBar.updateNotification(r.statusBarKey, n);
}

第五步:之所以能呼叫 mStatusBar.updateNotification(),是因為在 ServerThread 中建立 NotificationManagerService 時,就把它依賴的 StatusBarManagerService 作為建構參數傳了進來。

NotificationManagerService(Context context, StatusBarManagerService statusBar, LightsService lights) {
}

第六步:StatusBarManagerService 中的 updateNotification 方法會進一步呼叫 mBar.updateNotification()。

public void updateNotification(IBinder key, StatusBarNotification notification) {
    synchronized (mNotifications) {
        mNotifications.put(key, notification);
        mBar.updateNotification(key, notification);
    }
}

第七步:mBar 是一個 IStatusBar 型別的物件,透過 registerStatusBar 方法傳入。

volatile IStatusBar mBar;
public void registerStatusBar(IStatusBar bar, StatusBarIconList iconList, List notificationKeys, List notifications, int switches[], List binders) {
}

第八步:這個 registerStatusBar 由 SystemUI 中的 BaseStatusBar 呼叫,傳入的 mCommandQueue 正是那個 IStatusBar 物件。

public void start() {
    mBarService.registerStatusBar(mCommandQueue, iconList, notificationKeys, notifications, switches, binders);
}

第九步:CommandQueue 繼承自 IStatusBar.Stub,內部定義了一連串回呼函式,這些回呼都由 BaseStatusBar 實作。

public class CommandQueue extends IStatusBar.Stub {
    public interface Callbacks {
        public void addNotification(IBinder key, StatusBarNotification notification);
        public void updateNotification(IBinder key, StatusBarNotification notification);
        public void removeNotification(IBinder key);
        // ...其餘回呼方法
    }
}
public abstract class BaseStatusBar extends SystemUI implements CommandQueue.Callbacks {
}

第十步:於是當 StatusBarManagerService 呼叫 mBar.updateNotification 時,最終會落到 BaseStatusBar 實作的 updateNotification 方法,把 Notification 顯示在狀態列上。

歸根結底,整個過程主要圍繞 NotificationManagerService 與 StatusBarManagerService 的交互展開:

第一步:系統啟動時在 ServerThread 中生成兩個服務,並把 StatusBarManagerService 作為參數傳入 NotificationManagerService 的建構方法;同時 SystemUI 中的 BaseStatusBar 實作 CommandQueue.Callbacks,並呼叫 registerStatusBar 把 CommandQueue 傳入。

第二步:Activity 呼叫 notify,NotificationManagerService 把 Notification 封裝成 StatusBarNotification,再呼叫 StatusBarManagerService 的 updateNotification()。

第三步:StatusBarManagerService 呼叫 CommandQueue 實作的回呼函式,最終把 Notification 呈現在 SystemUI 之上。

下一篇我們將聊聊如何攔截 Notification 的內容,進而實現採集第三方播放器播放音樂的資訊。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved