Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Android system Global Touch event listening example Code

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "Android system global touch event monitoring example code". In daily operation, I believe that many people have doubts about the Android system global touch event monitoring example code. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "Android system global touch event monitoring example code". Next, please follow the editor to study!

Global Touch event Monitoring in Android system

Android touch global monitoring means that touch events can be obtained in any interface after invoking monitoring.

To achieve this function, you must modify the source code to add a new interface, because the system does not expose this method by default.

Source code

The class and related code that listens for system global touch events:

Frameworks\ base\ services\ core\ 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);}

The first parameter: is the medium PointerEventListener interface

There are MotionEvent objects that contain click events, such as DOWN, UP, MOVING, and other information.

Package android.view; public interface WindowManagerPolicyConstants {interface PointerEventListener {void onPointerEvent (MotionEvent motionEvent);}}

The second parameter, screen id, normally uses 0 to indicate the home screen id. Some devices need to pay attention to this only if they have a screen or a second screen.

Here is how to register for this service

1. Bind this system service. This method does not work.

Because the service's aidl interface IWindowManager does not expose this method

The registerPointerEventListener method is defined in another internal interface, 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. Get the WindowManagerFuncs object, which can be obtained in many ways in the source code.

Reference:

Frameworks\ base\ services\ core\ java\ com\ android\ server\ policy\ PhoneWindowManager.javapublic PhoneWindowManager extends AbsPhoneWindowManager implements WindowManagerPolicy, IHwPhoneWindowManagerInner {public WindowManagerFuncs getWindowManagerFuncs () {return mWindowManagerFuncs;}}

WindowManagerFuncs can be directly new in the source code, which can be used as follows:

PhoneWindowManager phoneWindowManager = new PhoneWindowManager (); WindowManagerFuncs windowManagerFuncs = phoneWindowManager.getWindowManagerFuncs (); windowManagerFuncsEx.registerPointerEventListener (listener, Display.DEFAULT_DISPLAY)

3. Add aidl callback to Huawei Emui source code

WindowManagerEx has a channel to send data directly to WindowManagerService and can monitor data

(1) add aidl API

Vendor\ huawei\ Emui\ frameworks\ hwCommInterface\ base\ core\ java\ com\ huawei\ android\ app\ IHwPointEventCallback.aidlpackage com.huawei.android.app; import android.view.MotionEvent; oneway interface IHwPointEventCallback {void onPointerEvent (in MotionEvent motionEvent);}

(2) Modification of 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; / / pass the listening object public static void setPointerEventListener (IHwPointEventCallback listener) {Log.i (LOG_TAG, "setPointerEventListener listener =" + listener) to WindowManagerService; IBinder windowManagerBinder = WindowManagerGlobal.getWindowManagerService (). AsBinder () If (windowManagerBinder! = null) {Parcel data = Parcel.obtain (); Parcel reply = Parcel.obtain (); try {data.writeInterfaceToken ("android.view.IWindowManager"); / / pass aidl listener object data.writeStrongBinder (listener! = null? Listener.asBinder (): null); / / send 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) receive data in WindowManagerService and do actual monitoring

Based on the idea of not modifying the source code as much as possible, there is a subclass HwWindowManagerService of WindowManagerService in Emui, so you can modify the code in the subclass.

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 / / receive data from 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 }} / / create a unique listener object in 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 ());}}; / / add setting touch listening method 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) {/ / actually called to the parent's registered touch event method registerPointerEventListener (mPointerEventListener, Display.DEFAULT_DISPLAY) } else {/ / the method unregisterPointerEventListener (mPointerEventListener, Display.DEFAULT_DISPLAY) that is actually called to the parent class to unregister touch events;}}

Method 3 can monitor the global touch events of the system in ordinary app.

Because app can rely on Emui's emui_addons.jar

Call to some of the classes, such as WindowManagerEx, to listen for global touch events.

Other system environments can refer to the above implementation according to the actual situation.

At this point, the study on the "Android system global touch event monitoring example code" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report