SoFunction
Updated on 2025-04-07

Example of hook click event under Android

Hook is a kind of idea, that is, replace the original event with our own event, so that we can do some in-depth processing. The purpose is to not modify the original code, and to avoid missing N many types of processing.

Recently, you need to set statistics burial points in existing apps. If you bury it in the business code, it seems that the coupling degree is too high. So I decided to use hook method to bury the event.

Here we first record the basic process of click event hook.

1. Create a proxy class implementation first to perform subsequent processing after clicking.

import ;

/**
  * Implement click monitoring
  */
public class OnClickListenerProxy implements {
  private  mOriginalListener;

  //Pass the original OnClickListener directly into the constructor  public OnClickListenerProxy( originalListener) {
    mOriginalListener = originalListener;
  }

  @Override public void onClick(View v) {
    if (mOriginalListener != null) {
      (v);
    }
    ("LOGCAT","hooked!");
  }
}

2. Hook through Java's reflection mechanism

public static void hookOnClickListener(View view) {
    try {
      // Get the ListenerInfo object of View      Method getListenerInfo = ("getListenerInfo");
      //Modify getListenerInfo to be accessible (getListenerInfo in View is not public)      (true);
      Object listenerInfo = (view);
      // Get the original OnClickListener object      Class<?> listenerInfoClz = ("$ListenerInfo");
      Field mOnClickListener = ("mOnClickListener");
      (true);
       originOnClickListener = () (listenerInfo);
      // Replace the original OnClickListener with a custom OnClickListener       hookedOnClickListener = new OnClickListenerProxy(originOnClickListener);
      (listenerInfo, hookedOnClickListener);
    } catch (Exception e) {
      ("LOGCAT","hook clickListener failed!", e);
    }
  }

3. Call the hookOnClickListener above after the event you need to hook

Button btnSend = (Button) findViewById(.btn_send);
    (new () {
      @Override
      public void onClick(View v) {
        ("onClick");
      }
    });
    (btnSend);

4. As a statistical burial point, it is necessary to bring some parameters

Set parameters in the onClick of the original business code

private  clickBtn = new (){
    @Override
    public void onClick(View v) {
      Map map = new HashMap();
      ("name",().getName());
      ((),map);
      (v);
    }
  };

Receive parameters in custom proxy onClick

@Override public void onClick(View v) {
    if (mOriginalListener != null) {
      (v);
    }
//    ("LOGCAT","hooked!"+());
    //Get the parameters passed before    Object obj = (());
    ("LOGCAT","hooked!"+()+"_"+());
  }

At this point, you can add subsequent operations at will to the hook without changing the original logic code.

Related github address:/codeqian/android-class-lib/tree/master/utilDemo/app/src/main/java/Hook

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.