Implementation of phone call hanging in Android Development
Before Android 1.5, it was very easy to call TelephonyManager's endCall () method. However, after Android, for the sake of mobile phone security, Google engineers have hidden the endCall method. Therefore, you can use the Reflection Method to execute the endCall method. The specific implementation is as follows:
TelephonyManager is described in the source code as follows: Context. getSystemService (Context. TELEPHONY_SERVICE)}, which can be obtained by calling TELEPHONY_SERVICE system service.
registerService(TELEPHONY_SERVICE, new ServiceFetcher() { public Object createService(ContextImpl ctx) { return new TelephonyManager(ctx.getOuterContext()); }});
Android. OS. ServiceManager has the getService method.
/** * Returns a reference to a service with the given name. * * @param name the name of the service to get * @return a reference to the service, or null if the service doesn't exist */ public static IBinder getService(String name) { return null; }
The android. telephony. TelephonyManager class is used to represent the ITelephony interface. You can see that the endCall is hidden.
/** * Interface used to interact with the phone. Mostly this is used by the * TelephonyManager class. A few places are still using this directly. * Please clean them up if possible and use TelephonyManager insteadl. * * {@hide} */interface ITelephony { /** * Dial a number. This doesn't place the call. It displays * the Dialer screen. * @param number the number to be dialed. If null, this * would display the Dialer screen with no number pre-filled. *//** * End call or go to the Home screen * * @return whether it hung up */ boolean endCall();
We know that using the binding service can call the method in it, an IBinder object will be returned, and IBinder can call the methods in the service.
TelephonyManager is actually the proxy object of the system telephone service. It obtains IBinder through aidl and further encapsulates it. Proxy method.
TelephonyManager ------------------ IBinder ------------------------- System Service
Proxy object aidl
So the implementation requires the following steps:
1. Load the ServiceManager class through reflection.
2. Get the IBinder object and execute the getService method.
3. Create a com. android. internal. telephony package in the project (the package name cannot be changed), and then copy ITelephony. aidl. In the new android. telephony package, copy the Process Communication Interface files of NeighboringCellInfo. aidl.
4. Call the endCall method.
The implementation method is as follows:
Private void endCall () {// IBinder iBinder = ServiceManager. getService (TELEPHONY_SERVICE); // ServiceManager is hidden by the system, so you can only use the reflection method to get try {// load ServiceManager's bytecode Class
Clazz = CallSMSSafeService. class. getClassLoader (). loadClass ("android. OS. serviceManager "); Method method = clazz. getDeclaredMethod ("getService", String. class); IBinder iBinder = (IBinder) method. invoke (null, TELEPHONY_SERVICE); ITelephony. stub. asInterface (iBinder ). endCall ();} catch (Exception e) {e. printStackTrace (); Log. I (TAG, "phone interception exception ");}}