先贴上这些源码里面相关的文件:

framework/base/core/java/android/app/NotificationManager.java

framework/base/services/java/com/android/server/NotificationManagerService.java{@hide} extends INotificationManager.Stub

framework/base/services/java/com/android/server/StatusBarManagerService.java  extends IStatusBarService.Stub

framework/base/core/java/com/android/internal/statusbar/StatusBarNotification  implements Parcelable

framework/base/core/java/com/android/internal/statusbar/IStatusBar.aidl

framework/base/core/java/com/android/internal/statusbar/IStatusBarService.aidl

framework/base/core/java/com/android/internal/statusbar/StatusBarNotification.aidl

framework/base/packages/SystemUI/src/com/android/systemui/statusbar/StatusBarService.java extends Service implements CommandQueue.Callbacks

framework/base/packages/SystemUI/src/com/android/systemui/statusbar/CommandQueue.java extends IStatusBar.Stub

1>.系统启动的时候:framework/base/services/java/com/android/server/SystemServer.java中:

try{

Slog.i(TAG,"Status Bar");

statusBar= newStatusBarManagerService(context);

ServiceManager.addService(Context.STATUS_BAR_SERVICE, statusBar);

}catch(Throwable e) {

Slog.e(TAG,"Failure starting StatusBarManagerService", e);

}try{

Slog.i(TAG,"Notification Manager");

notification= newNotificationManagerService(context, statusBar, lights);

ServiceManager.addService(Context.NOTIFICATION_SERVICE, notification);

}catch(Throwable e) {

Slog.e(TAG,"Failure starting Notification Manager", e);

}

这段代码是注册状态栏管理和通知管理这两个服务。

2>.在StatusBarManagerService.java中,有addNotification,removeNotification,updateNotification等方法用于管理传递给他的通知对象。这个类是一些管理方法,实际执行相关动作的是在IStatusBar.java里面,这个是framework/base/core/java/com/android/internal/statusbar/IStatusBar.aidl自动生成的用于IPC的类。

拿addNotification方法示范:

publicIBinder addNotification(StatusBarNotification notification) {synchronized(mNotifications) {

IBinder key= newBinder();

mNotifications.put(key, notification);if (mBar != null) {try{

mBar.addNotification(key, notification);

}catch(RemoteException ex) {

}

}returnkey;

}

}

这里的mBar其实就是IStatusBar的实例

volatile IStatusBar mBar;

为了防止NPE,每次使用mBar都先判断是否为null,mBar是在方法registerStatusBar中传递进来的。

public voidregisterStatusBar(IStatusBar bar, StatusBarIconList iconList,

List notificationKeys, Listnotifications) {

enforceStatusBarService();

Slog.i(TAG,"registerStatusBar bar=" +bar);

mBar=bar;synchronized(mIcons) {

iconList.copyFrom(mIcons);

}synchronized(mNotifications) {for (Map.Entrye: mNotifications.entrySet()) {

notificationKeys.add(e.getKey());

notifications.add(e.getValue());

}

}

}

framework/base/packages/SystemUI/src/com/android/systemui/statusbar/CommandQueue.java实现IStatusBar.java接口,

framework/base/packages/SystemUI/src/com/android/systemui/statusbar/StatusBarService.java提供IStatusBar相关服务。

CommandQueue.java中,IStatusBar.java里面对应的方法是用callback的形式调用的,callback的实现当然就在对应的服务提供类也就是StatusBarService.java中提供的啦。

CommandQueue.java中:

public voidaddNotification(IBinder key, StatusBarNotification notification) {synchronized(mList) {

NotificationQueueEntry ne= newNotificationQueueEntry();

ne.key=key;

ne.notification=notification;

mHandler.obtainMessage(MSG_ADD_NOTIFICATION,0, 0, ne).sendToTarget();//这句话对应的mHandler执行语句是://final NotificationQueueEntry ne = (NotificationQueueEntry)msg.obj;//mCallbacks.addNotification(ne.key, ne.notification);//也就是调用回调函数里面的addNotification。

}

}

在StatusBarService.java中:

mCommandQueue = new CommandQueue(this, iconList);//StatusBarService实现了CommandQueue中的CommandQueue.Callbacks接口

mBarService =IStatusBarService.Stub.asInterface(

ServiceManager.getService(Context.STATUS_BAR_SERVICE));try{//将IStatusBar实现类的对象传递到StatusBarManagerService.java中,这里的mCommandQueue就是上面对应的mBar啦。

mBarService.registerStatusBar(mCommandQueue, iconList, notificationKeys, notifications);

}catch(RemoteException ex) {//If the system process isn't there we're doomed anyway.

}

最终执行状态栏更新通知等事件都是在实现的CommandQueue.Callbacks里面执行。还是以addNotification为例:

public voidaddNotification(IBinder key, StatusBarNotification notification) {boolean shouldTick = true;if (notification.notification.fullScreenIntent != null) {

shouldTick= false;

Slog.d(TAG,"Notification has fullScreenIntent; sending fullScreenIntent");try{

notification.notification.fullScreenIntent.send();

}catch(PendingIntent.CanceledException e) {

}

}

StatusBarIconView iconView=addNotificationViews(key, notification);if (iconView == null) return;//。。。以下省略N字。

大致流程就是:调用StatusBarManagerService.java中的addNotification方法->(mBar不为空的话)执行mBar.addNotification(key, notification);->对应的是CommandQueue中的addNotification(IBinder key, StatusBarNotification notification)->CommandQueue中的mCallbacks.addNotification(ne.key, ne.notification);->StatusBarService中的addNotification。

3>.上面是提供相关功能的一些类,具体的notification的管理类是framework/base/services/java/com/android/server/NotificationManagerService.java,从该类的定义public class NotificationManagerService extends INotificationManager.Stub可以知道

他是用来实现接口中INotificationManager中定义的相关方法并向外部提供服务的类。主要向外提供public void enqueueNotificationWithTag(String pkg, String tag, int id, Notification notification,int[] idOut)方法。该方法实际上是调用public void enqueueNotificationInternal(String pkg, int callingUid, int callingPid,String tag, int id, Notification notification, int[] idOut),他里面提供了notification的具体处理方法。

摘取部分代码片段看看:

if (notification.icon != 0) {

StatusBarNotification n= newStatusBarNotification(pkg, id, tag,

r.uid, r.initialPid, notification);if (old != null && old.statusBarKey != null) {

r.statusBarKey=old.statusBarKey;long identity =Binder.clearCallingIdentity();try{

mStatusBar.updateNotification(r.statusBarKey, n);

}finally{

Binder.restoreCallingIdentity(identity);

}

}else{//省略。。。

当判断好需要更新通知的时候调用mStatusBar.updateNotification(r.statusBarKey, n);方法,这个就是StatusBarManagerService.java中的addNotification方法,这样就进入上面所说的处理流程了。

4>. 在3中的NotificationManagerService.java是管理notification的服务,服务嘛就是用来调用的,调用他的就是大家熟悉的NotificationManager了。

在NotificationManager.java中,有一个隐藏方法,用来得到INotificationManager接口对应的服务提供类,也就是NotificationManagerService了。

/**@hide*/

static publicINotificationManager getService()

{if (sService != null) {returnsService;

}

IBinder b= ServiceManager.getService("notification");

sService=INotificationManager.Stub.asInterface(b);returnsService;

}

再看看更熟悉的notify方法,其实是执行:

public void notify(String tag, intid, Notification notification)

{int[] idOut = new int[1];

INotificationManager service=getService();

String pkg=mContext.getPackageName();if (localLOGV) Log.v(TAG, pkg + ": notify(" + id + ", " + notification + ")");try{

service.enqueueNotificationWithTag(pkg, tag, id, notification, idOut);if (id != idOut[0]) {

Log.w(TAG,"notify: id corrupted: sent " + id + ", got back " + idOut[0]);

}

}catch(RemoteException e) {

}

}

ervice.enqueueNotificationWithTag(pkg, tag, id, notification, idOut);也就是3中提到的那个对外公开的服务方法了,这样就进入了上面提到的处理流程了。

mysql数据变化通通知机制_深入理解Notification机制相关推荐

  1. mysql数据转存到时序数据库_干货丨如何高速迁移MySQL数据到时序数据库DolphinDB...

    DolphinDB提供了两种导入MySQL数据的方法:ODBC插件和MySQL插件.我们推荐使用MySQL插件导入MySQL数据,因为它的速度比ODBC导入更快,导入6.5G数据,MySQL插件的速度 ...

  2. session实现机制_如何理解php session运行机制

    php session运行机制就是客户端将session id传入到服务器中,服务器再根据session id找到对应的文件并将其反序列化得到session值,然后保存的时候先序列化再写入今天将要分享 ...

  3. 工作中用到的java反射机制_(转)JAVA-反射机制的使用

    Java反射机制的实现原理 反射机制:所谓的反射机制就是java语言在运行时拥有一项自观的能力.通过这种能力可以彻底的了解自身的情况为下一步的动作做准备.下面具体介绍一下java的反射机制.这里你将颠 ...

  4. 建立完善的员工晋升机制_【员工晋升机制】多渠道员工晋升机制如何建立

    北京华恒智信人力资源顾问有限公司 [员工晋升机制]多渠道员工晋升机制如何建立 引言: 员工晋升机制是员工由较低层级职位上升到较高层级职位的过程, 合理的员工晋 升机制可以实现良好的资源配置, 使合适的 ...

  5. 怎么监控mysql数据变化_实时监控mysql数据库变化

    对于二次开发来说,很大一部分就找找文件和找数据库的变化情况 对于数据库变化.还没有发现比较好用的监控数据库变化监控软件. 今天,我就给大家介绍一个如何使用mysql自带的功能监控数据库变化 1.打开数 ...

  6. mysql 事务 返回插入的值_深入理解mysql事务:事务机制的实现原理

    作者:logan 出自:SegmentFault 思否 一.事物的四个特性(ACID) 原子性(Atomicity):操作这些指令时,要么全部执行成功,要么全部不执行.只要其中一个指令执行失败,所有的 ...

  7. 存储mysql数据存在特殊字符时处理_转义 存储数据时特殊符号的处理

    function url_base64_encode($str){ //将这个方法处理后的数据可以存储,不会有特殊符号 if($str=="") return "&quo ...

  8. 如何改善mysql数据装载操作效率的方法_详述如何提高MySQL中数据装载效率

    摘抄自:http://database.ctocio.com.cn/153/9232653.shtml 很多时候关心的是优化SELECT 查询,因为它们是最常用的查询,而且确定怎样优化它们并不总是直截 ...

  9. 假设mysql数据表t1有字段_使用ROMA Connect集成数据

    概述 ROMA Connect支持接入多种类型的数据源,并通过数据集成任务实现源端到目标端的数据集成转换.ROMA Connect支持相同结构数据之间进行集成转换,也支持异构数据之间进行集成转换. 本 ...

最新文章

  1. HttpComponents
  2. 服务器开启虚拟机就死机,解决ESXi服务器上磁盘锁导致虚拟机卡死的问题
  3. 『cURL』curl: (6) Could not resolve host无法解析主机地址
  4. 13_clickhouse,Merge引擎,File引擎,External Data引擎,External Data引擎,Null Engine,URL引擎,Memory、Set、Buffer
  5. SQL数据库权限授予grant
  6. 有什么好的Java自学教程视频,适合初学者
  7. 微带线特性阻抗计算公式_利用HFSS计算微带线的特性阻抗
  8. ubuntu修改顶栏颜色
  9. Underlay网络:如何立住可靠又支持大规模无收敛的“人设”
  10. 三星:计划将UTG可折叠面板对外出售
  11. Memcahce和Redis比较
  12. DropDownList的项按字母顺序排列
  13. DevC++ 软件下载及安装教程(详细、具体)
  14. 基于 SpringBoot+Vue+Java 实现酒店客房管理系统
  15. 基于Java的物资租赁管理系统 MYSQL
  16. mysql三m架构为什么_AnalyticDB for MySQL 3.0 技术架构解析
  17. python 换脸 github_如何用200行Python代码“换脸”
  18. python3爬虫进阶之自动登录网易云音乐并爬取指定歌曲评论
  19. 嵌入式linux解决方法 | u-boot NFS下载文件报错:Loading: *** ERROR: File lookup fail
  20. 数字逻辑综合工具实践-DC-07 ——综合优化(二)和RTL coding 和DFT

热门文章

  1. Spring Boot框架中使用Jackson的处理总结
  2. JAVA WEB篇4——Filter、Listener
  3. uniapp动态修改样式_掌握Photoshop图层样式技术
  4. JDBC——编程式事务的实现逻辑
  5. Shiro————会话管理
  6. Java 多线程 —— 常用并发容器
  7. 命名空间中不存在名称_原木定制中不开裂的木材真的存在吗?
  8. mysql撤销用户授权_mysql用户授权及撤销
  9. pl sql 连接mysql_PL/SQL 连接mysql步骤
  10. python绘制如下图形、小三角形边长20_python二级操作题与分析(7)