編輯:關於 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, ListnotificationKeys, 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 的內容,進而實現採集第三方播放器播放音樂的資訊。
Android JNI 中的 JNIEnv 解析
JNIEnv 是 JNI 開發中非常關鍵的概念。它代表目前執行緒的 Java 執行環境,也是 C 或 C++ 程式呼叫 Java 方法、操作 Java 物件時必須依靠的入
React Native 與 Android 原生的螢幕適配對照實作
從初次接觸 RN 到現在終於能寫出點東西,值得給自己一個肯定。學習這條路還長,廢話少說,直接進主題。 RN 的螢幕適配,說真的比 Android 原生簡單不少——不得不佩
Android 實戰教程第十四話:模仿金山助手打造桌面 Widget 小工具
這一話的目標是從零做出一個可以放進桌面、能定時更新的 App Widget,做法參照 Google 官方 API 的說明,介面則模仿金山助手那種程序管理小工具。 先看官
Android 逆向入門實戰:動手破解三個練習程式的完整筆記
首先要感謝魚C論壇的 cbs 大神,這篇筆記的起點正是他的教學影片。我按照他的示範,親手把這三個練習用的 Crackme 從頭到尾破解了一遍。願意把技術無私分享出來的人其