Facing problem in adding AIDL classes in android?
It's not necessary to add ITelephony.aidl class in android, it's convenience to add them.
So the question is how to use AIDL class in android project?
The answer is Java Reflection.If you know what is there and what it does, you can use reflection for get them even they are hidden.
For example:- Suppose you want to create a program in which you need to disconnect the call using ITelephony class then there is a method endCall() in that class. Below are code snippet how to use AIDL class using java reflection :-
Java :-
TelephonyManager tm = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);Class c = Class.forName(tm.getClass().getName());Method m = c.getDeclaredMethod("getITelephony");m.setAccessible(true);Object telephonyService = m.invoke(tm); // Get the internal ITelephony objectc = Class.forName(telephonyService.getClass().getName()); // Get its classm = c.getDeclaredMethod("endCall"); // Get the "endCall()" methodm.setAccessible(true); // Make it accessiblem.invoke(telephonyService); // invoke endCall()
Kotlin :-
val telephonyService: Any
val telephony = context?.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
try{
var c = Class.forName(telephony.javaClass.name)
var m = c.getDeclaredMethod("getITelephony")
m.isAccessible = true
telephonyService = m.invoke(telephony) as Any
c = Class.forName(telephonyService.javaClass.name) // Gets its class
m = c.getDelaredMethod("endCall") // Get the "endCall()' method
m.isAccessible = true //Make it accessible
m.invoke(telephonyService) //invoke endCall()
} catch (e: Exception){
e.printStackTrace()
}
}
No comments:
Post a Comment