NotificationManager与NotificationManagerService交互流程
获取NotificationManager
- SDK提供API
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
- 接着调用ContextImpl
class ContextImpl extends Context {
@Override
public Object getSystemService(String name) {
return SystemServiceRegistry.getSystemService(this, name);
}
}
- 接着调用SystemServiceRegistry
final class SystemServiceRegistry {
static {
//初始化 SYSTEM_SERVICE_FETCHERS
}
private static final HashMap<String, ServiceFetcher<?>> SYSTEM_SERVICE_FETCHERS =
new HashMap<String, ServiceFetcher<?>>();
public static Object getSystemService(ContextImpl ctx, String name) {
ServiceFetcher<?> fetcher = SYSTEM_SERVICE_FETCHERS.get(name);
return fetcher != null ? fetcher.getService(ctx) : null;
}
}
NotificationManager#notify发送通知
public class NotificationManager {
//最终调用到这里
public void notifyAsUser(String tag, int id, Notification notification, UserHandle user) {
1. INotificationManager service = getService();
2. service.enqueueNotificationWithTag添加到NotificationManagerService通知队列里
}
public void cancelAsUser(String tag, int id, UserHandle user) {
1. INotificationManager service = getService();
2. service.cancelNotificationWithTag从NotificationManagerService通知队列里移除
}
//获取跨进程INotificationManager
static public INotificationManager getService() {
if (sService != null) {
return sService;
}
IBinder b = ServiceManager.getService("notification");
sService = INotificationManager.Stub.asInterface(b);
return sService;
}
}