手把手教你Android全局触摸事件监听
android系统全局触摸事件监听
android触摸全局监听指的是调用监听后在任何界面都能获取到触摸事件。
要实现这个功能必须要修改源码添加新的接口,因为系统默认是不暴露这个方法的。
源码
监听系统全局触摸事件的类和相关代码:
frameworks\base\services\core\java\com\android\server\wm\windowmanagerservice.java @override public void registerpointereventlistener(pointereventlistener listener, int displayid) { slog.i(tag, "registerpointereventlistener pointereventlistener = " + listener); synchronized (mgloballock) { final displaycontent displaycontent = mroot.getdisplaycontent(displayid); if (displaycontent != null) { displaycontent.registerpointereventlistener(listener); } } } @override public void unregisterpointereventlistener(pointereventlistener listener, int displayid) { synchronized (mgloballock) { final displaycontent displaycontent = mroot.getdisplaycontent(displayid); if (displaycontent != null) { displaycontent.unregisterpointereventlistener(listener); } } }
第一个参数:是中pointereventlistener接口,
里面有motionevent
对象含有点击事件,比如down、up、moving
等其他信息。
package android.view; public interface windowmanagerpolicyconstants { interface pointereventlistener { void onpointerevent(motionevent motionevent); } }
第二个参数,屏幕id,正常用0 ,表示主屏幕id。有些设备有投屏或者第二屏才需要关注这个。
下面介绍如何注册这个服务
1、绑定这个系统服务,这个方法行不通
因为这个服务的aidl接口iwindowmanager,并没有暴露这个方法
registerpointereventlistener方法定义在另一个内部接口 windowmanagerfuncs 中
public interface windowmanagerpolicy extends windowmanagerpolicyconstants { public interface windowmanagerfuncs { /** register a system listener for touch events */ void registerpointereventlistener(pointereventlistener listener, int displayid); /** unregister a system listener for touch events */ void unregisterpointereventlistener(pointereventlistener listener, int displayid); } }
2、获取windowmanagerfuncs对象,该对象获取的方式在源码中有多种
参考:
frameworks\base\services\core\java\com\android\server\policy\phonewindowmanager.java public phonewindowmanager extends absphonewindowmanager implements windowmanagerpolicy, ihwphonewindowmanagerinner{ public windowmanagerfuncs getwindowmanagerfuncs(){ return mwindowmanagerfuncs; } }
windowmanagerfuncs在源码中是可以直接new的,使用如下:
phonewindowmanager phonewindowmanager = new phonewindowmanager(); windowmanagerfuncs windowmanagerfuncs = phonewindowmanager.getwindowmanagerfuncs(); windowmanagerfuncsex.registerpointereventlistener(listener, display.default_display);
3、在华为emui源码添加aidl回调
windowmanagerex有通道直接发送数据到windowmanagerservice并可以进行数据监听
(1)添加aidl接口
vendor\huawei\emui\frameworks\hwcomminterface\base\core\java\com\huawei\android\app\ihwpointeventcallback.aidl package com.huawei.android.app; import android.view.motionevent; oneway interface ihwpointeventcallback { void onpointerevent(in motionevent motionevent); }
(2)windowmanagerex的修改
vendor\huawei\emui\frameworks\hwext\hwext\framework\src\com\huawei\android\app\windowmanagerex.java private final int transaction_set_pointer_event_listener = android.os.ibinder.first_call_transaction + 2100; //给windowmanagerservice传递监听对象 public static void setpointereventlistener(ihwpointeventcallback listener) { log.i(log_tag, "setpointereventlistener listener = " + listener); ibinder windowmanagerbinder = windowmanagerglobal.getwindowmanagerservice().asbinder(); if (windowmanagerbinder != null) { parcel data = parcel.obtain(); parcel reply = parcel.obtain(); try { data.writeinterfacetoken("android.view.iwindowmanager"); //传递aidl监听对象 data.writestrongbinder(listener != null ? listener.asbinder() : null); //发送 windowmanagerbinder.transact(transaction_set_pointer_event_listener, data, reply, 0); } catch (remoteexception e){ log.e(log_tag, "setpointereventlistener exception is " + e.getmessage()); } finally { data.recycle(); reply.recycle(); } } else { log.w(log_tag, "setpointereventlistener windowmanagerbinder is null"); } }
(3)在windowmanagerservice中接收数据并做实际监听
基于尽量不修改源码的理念,emui中有windowmanagerservice的子类hwwindowmanagerservice,在子类中修改代码即可。
vendor\huawei\emui\frameworks\base\services\java\huawei\com\android\server\wm\hwwindowmanagerservice.java hwwindowmanagerservice extends windowmanagerservice private final int transaction_set_pointer_event_listener = android.os.ibinder.first_call_transaction + 2100; private ihwpointeventcallback mihwpointeventcallback = null; //接收windowmanagerex传递过来的数据 public boolean ontransact(int code, parcel data, parcel reply, int flags) throws remoteexception { switch (code) { case transaction_set_pointer_event_listener: data.enforceinterface("android.view.iwindowmanager"); ihwpointeventcallback observer = ihwpointeventcallback.stub.asinterface(data.readstrongbinder()); setpointereventlistener(observer); reply.writenoexception(); return true; } } //在service中创建唯一的监听对象 private pointereventlistener mpointereventlistener = new pointereventlistener() { @override public void onpointerevent(motionevent motionevent) { if(mihwpointeventcallback != null) { try { mihwpointeventcallback.onpointerevent(motionevent); } catch (remoteexception e) { slog.e(tag, "mihwpointeventcallback error = " + e.getmessage()); } } } }; //添加设置触摸监听方法 private void setpointereventlistener(ihwpointeventcallback listener) { slog.i(tag, "setpointereventlistener pointereventlistener = " + listener); int uid = binder.getcallinguid(); if(uid != process.system_uid){ slog.e(tag, "setpointereventlistener uid must be "+ process.system_uid +",but now uid = " + uid); return; } mihwpointeventcallback = listener; if(listener != null) { //实际调到父类的注册触摸事件的方法 registerpointereventlistener(mpointereventlistener, display.default_display); } else { //实际调到父类的反注册触摸事件的方法 unregisterpointereventlistener(mpointereventlistener, display.default_display); } }
方法3可以实现在普通app中监听到系统的全局触摸事件,
因为app可以依赖emui的emui_addons.jar,
调用到里面的部分类,比如windowmanagerex,就可以监听全局触摸事件。
其他系统环境可以根据实际情况参考上面的实现。
共勉:看得更多才知道还有更多还没看过。
到此这篇关于手把手教你android全局触摸事件监听的文章就介绍到这了,更多相关android触摸监听内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
推荐阅读