1, 基本概念
PhoneWindowManager也是运行于systemserver线程中,在Event事件分发之前处理,比如电源键。Event事件分发后,仅有包含Activity的apk线程才可以处理,如果apk中没有activity但是想处理Event事件怎么办呢?可以在PhoneWindowManager做做文章了。
在SystemServer中,
inputManager = new InputManagerService(context); wm = WindowManagerService.main(context, inputManager, mFactoryTestMode != FactoryTest.FACTORY_TEST_LOW_LEVEL, !mFirstBoot, mOnlyCore); ServiceManager.addService(Context.WINDOW_SERVICE, wm); ServiceManager.addService(Context.INPUT_SERVICE, inputManager); mActivityManagerService.setWindowManager(wm); inputManager.setWindowManagerCallbacks(wm.getInputMonitor()); // 消息分发之前的回调 inputManager.start();
InputManagerService的创建以及启动已经论述了,在此就不多说了。在
WindowManagerService中新建了PhoneWindowManager和InputMonitor,并且将设置为InputManagerService对象的回调对象。其实这2个类都和Event的处理有关,在Event分发之前进行处理,相当于截取,具体的细节就一层一层抽丝剥茧了。
PhoneWindowManager是没有对应的C/C++层代码了。
看下InputManagerService的setWindowManagerCallbacks方法,
public void setWindowManagerCallbacks(WindowManagerCallbacks callbacks) { mWindowManagerCallbacks = callbacks; }
因此, InputManagerService的变量mWindowManagerCallbacks指向InputMonitor对象。
2处理流程
InputDispatcher的notifyKey方法中,Event进队列之前,会调用interceptKeyBeforeQueueing方法,
mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags);
mPolicy变量是什么呢?通过查找,是NativeInputManager对象,
void NativeInputManager::interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags) { bool interactive = mInteractive.load(); if (interactive) { policyFlags |= POLICY_FLAG_INTERACTIVE; } if ((policyFlags & POLICY_FLAG_TRUSTED)) { nsecs_t when = keyEvent->getEventTime(); JNIEnv* env = jniEnv(); jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent); jint wmActions; if (keyEventObj) { wmActions = env->CallIntMethod(mServiceObj, gServiceClassInfo.interceptKeyBeforeQueueing, keyEventObj, policyFlags); if (checkAndClearExceptionFromCallback(env, "interceptKeyBeforeQueueing")) { wmActions = 0; } android_view_KeyEvent_recycle(env, keyEventObj); env->DeleteLocalRef(keyEventObj); } ••• }
在register_android_server_InputManager方法中,
int res = jniRegisterNativeMethods(env, "com/android/server/input/InputManagerService", gInputManagerMethods, NELEM(gInputManagerMethods)); ••• GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeQueueing, clazz, "interceptKeyBeforeQueueing", "(Landroid/view/KeyEvent;I)I");
很明显了,调用Java层InputManagerService的interceptKeyBeforeQueueing方法,后面就没有什么困难了,最后调用PhoneWindowManager的interceptKeyBeforeQueueing方法。
看下InputMonitor的方法,有一些也是同样的方法调用,关键是C/C++层的代码什么时候调用,觉得InputMonitor的主要目的还是解耦,防止PhoneWindowManager和InputManagerService太紧,
public void notifyLidSwitchChanged(long whenNanos, boolean lidOpen) { mService.mPolicy.notifyLidSwitchChanged(whenNanos, lidOpen); } public void notifyCameraLensCoverSwitchChanged(long whenNanos, boolean lensCovered) { mService.mPolicy.notifyCameraLensCoverSwitchChanged(whenNanos, lensCovered); } public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags) { return mService.mPolicy.interceptKeyBeforeQueueing(event, policyFlags); } public int interceptMotionBeforeQueueingNonInteractive(long whenNanos, int policyFlags) { return mService.mPolicy.interceptMotionBeforeQueueingNonInteractive( whenNanos, policyFlags); } public long interceptKeyBeforeDispatching( InputWindowHandle focus, KeyEvent event, int policyFlags) { WindowState windowState = focus != null ? (WindowState) focus.windowState : null; return mService.mPolicy.interceptKeyBeforeDispatching(windowState, event, policyFlags); } public KeyEvent dispatchUnhandledKey( InputWindowHandle focus, KeyEvent event, int policyFlags) { WindowState windowState = focus != null ? (WindowState) focus.windowState : null; return mService.mPolicy.dispatchUnhandledKey(windowState, event, policyFlags); } public int getPointerLayer() { return mService.mPolicy.windowTypeToLayerLw(WindowManager.LayoutParams.TYPE_POINTER) * WindowManagerService.TYPE_LAYER_MULTIPLIER + WindowManagerService.TYPE_LAYER_OFFSET; }
在InputDispatcher.cpp的中的Event入队列之后,dispatchKeyLocked方法中会调用doInterceptKeyBeforeDispatchingLockedInterruptible方法,然后调用NativeInputManager的interceptKeyBeforeDispatching方法,
nsecs_t delay = mPolicy->interceptKeyBeforeDispatching(commandEntry->inputWindowHandle, &event, entry->policyFlags);
最后会调用PhoneWindowManager的interceptKeyBeforeDispatching方法。
所以,如果apk中没有acitivity但是想监听Event事件怎么办呢?
在PhoneWindowManager的interceptKeyBeforeQueueing或者interceptKeyBeforeDispatching方法中稍加处理就可以了。
相关推荐
标题中的“博客https://blog.csdn.net/weixin_49457347/article/details/1236所需文件”表明这是一个与特定博客文章相关的压缩包,但没有提供足够的信息来直接解释博客的内容。从描述中也无法获取更多细节,它只是...
使用java语言,快速入门酷Q机器人编写。,快速入门,快速入门。 运行步骤: 1.必须依赖环境酷Q,然后安装插件CQP_HTTP ,启动运行即可 2.运行Java代码类:Mydemo1 3.QQ发送消息给登录酷Q的号 4.效果图如博客所示
https://blog.csdn.net/a6661314/article/details/124358796的实验文件
+viewpager+listview(adapterview)recyclerview scrollview webviewblog//blog.csdn.net/w7822938/article/details/47173047/demo-apk,可30452;
接前两篇文章,[小学生python游戏编程3----拼图游戏-准备](https://blog.csdn.net/fqfq123456/article/details/127173684),[小学生python游戏编程4----拼图游戏今天]...,上次写到可以打乱图片,拼成游戏界面,今天...
顺序表:https://blog.csdn.net/HAIIAKU/article/details/119302855 单链表:https://blog.csdn.net/HAIIAKU/article/details/119303873 顺序栈:https://blog.csdn.net/HAIIAKU/article/details/119304169 冒泡排序...
https://yydatav.blog.csdn.net/article/details/123826876 9【源码】数据可视化:基于 Echarts +Java SpringBoot 实现的动态实时大屏范例 - 酒店行业 https://yydatav.blog.csdn.net/article/details/123826729 ....
简单的学生信息管理系统,实现对用户类型的不同权限管理(增删改查). 文章介绍:https://blog.csdn.net/qq_56886142/article/details/122740969?spm=1001.2014.3001.5501
Socket实战——UDP连接:https://blog.csdn.net/haoranhaoshi/article/details/86601468 Socket实战——TCP连接:https://blog.csdn.net/haoranhaoshi/article/details/86601522 Socket实战——查询数据库:...
软件的安装和使用说明可以参考博文《手把手教你上手Proteus(下载安装+仿真51单片机程序)》( https://xiaohuisuper.blog.csdn.net/article/details/121993865 ) Proteus软件的功能很强大,它集合了电路仿真、PCB...
文章链接:https://blog.csdn.net/Jeankyw/article/details/131890436?spm=1001.2014.3001.5502
详情看我的我的文章https://blog.csdn.net/wsxybz/article/details/128486717
基于STM32F103单片机,通过PAJ7620手势识别模块实时检测并打印手势类型到串口调试助手。各位同学可基于该例程并根据自己的项目...更多详情,请浏览博客文章→https://xinso.blog.csdn.net/article/details/120616257
http://blog.csdn.net/zhangerqing/article/details/8239539 https://my.oschina.net/viakiba/blog/795797
资源包含文件:三份课程论文word+代码 运行程序提示可开始游戏。人人对战,二人轮流落子。界面显示提示信息当点轮到哪一方落子。详细介绍参考:https://blog.csdn.net/sheziqiong/article/details/126070235
HTC Vive 游戏——VR网球,HTC Vive 配合着手柄确实很好用,呈现效果也很好。(详情看我文章介绍:https://blog.csdn.net/m0_65636467/article/details/144704966)
CSDN博客链接:https://blog.csdn.net/mbs520/article/details/106987758 可通过指纹模块增删查改家庭成员的指纹信息,增删查改是否成功的相关信息显示在OLED屏幕上 2、在指纹匹配过程中,如果采集的指纹与指纹模块...
摘自:https://blog.csdn.net/qq_34218078/article/details/109591000
http://blog.csdn.net/xiangjianbo127/article/details/8610007 http://blog.csdn.net/xiangjianbo127/article/details/8610413 http://blog.csdn.net/xiangjianbo127/article/details/8610755 ...
https://blog.csdn.net/huangjhai/article/details/118854733