Access the itelephony hidden interface using the Java reflection mechanism

Source: Internet
Author: User

These two days I studied how to use a program to hang up the phone. We found that the endcall method was provided by phone in android1.0, and this method was set to private after Android 1.5.

So we have the following research:

/**
* Call itelephony's endcall () to end the call using the Java reflection mechanism.
*/
Private void endcall (){
// Initialize itelephony
Class <telephonymanager> C = telephonymanager. Class;
Method getitelephonymethod = NULL;
Try {
// Obtain all public/private/protected/Default
// Method function. If you only need to obtain the public method, you can call getmethod.
Getitelephonymethod = C. getdeclaredmethod ("getitelephony ",
(Class []) null );
// Whether to perform access check for the method object to be executed, that is, for public/private/protected/Default
// Whether we can access it. If the value is true, it indicates that the reflected object should cancel the Java language access check during use. The value is false.
// Indicates that the reflected object should implement Java access check.
Getitelephonymethod. setaccessible (true );
} Catch (securityexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (nosuchmethodexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}

Try {
Itelephony = (itelephony) getitelephonymethod. Invoke (
Tmanager, (object []) null );
Try {
Itelephony. endcall ();
} Catch (RemoteException e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
} Catch (illegalargumentexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (illegalaccessexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (invocationtargetexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}

 

 

 

/*************************************** **************************************** ***********/

The following excerpt uses Baidu space to describe how to use reflection for unit testing of private methods.

/*************************************** **************************************** ***********/

 

Use Security Manager

 

The combination of security manager and reflection mechanism can also achieve our goal. Java runtime relies on a security manager to check whether the calling code has sufficient permissions for a specific access. Specifically, the security manager is a Java. Lang. securitymanager class or a class that extends from this class, And it checks the operation permissions of some applications at runtime. In other words, all object access must be delegated to the security manager before executing its own logic. When access is controlled by the security manager, the application can only perform operations that are specifically permitted by the relevant security policy. Therefore, once started, the security manager can provide sufficient protection for the code. By default, the security manager is not set. Unless the Code explicitly installs a default or custom security manager, the access control check during runtime does not work. We can avoid this at runtime.
Java's access control check allows us to access non-public member variables or methods. To access the Non-Public members we need, we also need to use Java reflection technology. Java reflection is a powerful tool that allows us to configure Code while running without having to link source code between objects, making code more flexible. During compilation, the Java compiler ensures the private properties of private members, so that the private methods and private member variables of a class cannot be statically referenced by other classes. However, the Java reflection mechanism allows us to query and access variables and methods at runtime. Because reflection is dynamic, the check at compilation will no longer work.

The following code demonstrates how to use the security manager and reflection mechanism to access private variables.

Listing 3. Using the reflection mechanism, member variables of the callback class

// Obtain the value of the specified variable
Public static object
Getvalue (object instance, string fieldname)
Throws
Illegalaccessexception, nosuchfieldexception {


Field field = getfield (instance. getclass (), fieldname );
// If the parameter value is true, the access control check is disabled.

Field. setaccessible (true );
Return field. Get (instance );
}

// This method obtains the value of this variable based on the variable name.
Public static field
Getfield (class thisclass, string fieldname)
Throws nosuchfieldexception {

If (thisclass = NULL ){
Throw new nosuchfieldexception ("error
Field! ");

}
}

 

Getfield (instance. getclass (), fieldname) obtains object attributes through the reflection mechanism. If a security manager exists, the method first uses this and member. declared is used as a parameter to call the checkmemberaccess method of the security manager. here this is the parent class of this class or the member is determined. If the class is in the package, the method also uses the package name as the parameter to call the checkpackageaccess method of the security manager. Every call may cause securityexception. When the access is denied, both call methods generate
Securityexception exception.

The setaccessible (true) method disables the access control check by setting the parameter value to true, so that the variable can be called by other classes. We can extend a common basic class java. Lang. Reflect. accessibleobject in the class we write. This class defines a setaccessible method that enables or disables access detection for one of these classes. The problem with this method is that if the security manager is used, it will detect whether the code that is disabling access detection allows this. If not, the security manager throws an exception.

In addition to accessing private variables, we can also use this method to access private methods.

Listing 4. Using the reflection mechanism the member method of the callback class

Public static method
Getmethod (object instance, string methodname, class [] classtypes)
Throws
Nosuchmethodexception {


Method accessmethod = getmethod (instance. getclass (), methodname, classtypes );
// If the parameter value is true, the access control check is disabled.

Accessmethod. setaccessible (true );

Return accessmethod;
}

Private Static Method
Getmethod (class thisclass, string methodname, class [] classtypes)
Throws nosuchmethodexception {

If (thisclass = NULL ){
Throw new nosuchmethodexception ("error
Method! ");

} Try {
Return thisclass. getdeclaredmethod (methodname,
Classtypes );

} Catch (nosuchmethodexception e ){
Return getmethod (thisclass. getsuperclass (),
Methodname, classtypes );


}
}

 

The principle of obtaining a private method is the same as that of obtaining a private variable. When we get the function, we need to call it. In this case, we need to use the invoke () method to call the function. The code example is as follows:

 

// Call a method containing a single parameter
Public static object
Invokemethod (object instance, string methodname, object Arg)
Throws nosuchmethodexception,

Illegalaccessexception, invocationtargetexception {


Object [] ARGs = new object [1];

ARGs [0] = ARG;
Return invokemethod (instance,
Methodname, argS );
}
 
// Call a method containing multiple parameters
Public static object
Invokemethod (object instance, string methodname, object [] ARGs)
Throws nosuchmethodexception,

Illegalaccessexception, invocationtargetexception {

Class [] classtypes = NULL;
If (ARGs! = NULL ){

Classtypes = new class [args. Length];
For (INT I = 0;
I <args. length; I ++ ){
If (ARGs [I]! = NULL ){

Classtypes [I] = ARGs [I]. getclass ();

}

}

}
Return getmethod (instance,
Methodname, classtypes). Invoke (instance, argS );
}

Required permissions <uses-Permission Android: Name = "android. Permission. modify_phone_state"/>

 

Using security manager and reflection, you can access private members without modifying the source code, which brings great convenience for testing. This method can be compiled smoothly, especially during compilation. However, this method also has some disadvantages. The first is the performance problem. Reflection is much slower than direct code for field and method access. The second is permission issues. Some Java-related program code does not have the permission to modify the security manager. In this case, this method is invalid.

From http://blog.csdn.net/yangtaoJ2me/article/details/5752491

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.