我可以看到很多已经在SO上问过的问题,但是没有一个问题有正确的答案。
SO和博客上的大多数建议都在下面的代码段中提供建议:
var buttonDown = Intent(Intent.ACTION_MEDIA_BUTTON)
buttonDown.putExtra(Intent.EXTRA_KEY_EVENT,
KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK))
sendOrderedBroadcast(buttonDown, "android.permission.CALL_PRIVILEGED")
var buttonDown1 = Intent(Intent.ACTION_MEDIA_BUTTON)
buttonDown.putExtra(Intent.EXTRA_KEY_EVENT,
KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK))
sendOrderedBroadcast(buttonDown1, "android.permission.CALL_PRIVILEGED")
但是,它不起作用,并引发错误,指出第三方应用程序不允许使用“ CALL_PRIVILEGED”。
一些帖子和答案建议使用下面的代码段。
Runtime.getRuntime().exec("input keyevent " + Integer.toString(KeyEvent.KEYCODE_HEADSETHOOK))
但是不幸的是,这也没有帮助我。我知道我们有来自android框架的现成方法,可以接受Oreo以上api级的调用和结束调用。但是我也需要在oreo下实现此功能。
我发现下面的代码使用反射拒绝了呼叫,并且工作正常。
try {
var serviceManagerName = "android.os.ServiceManager"
var serviceManagerNativeName = "android.os.ServiceManagerNative"
var telephonyName = "com.android.internal.telephony.ITelephony"
var telephonyClass: Class<*>
var telephonyStubClass: Class<*>
var serviceManagerClass: Class<*>
var serviceManagerNativeClass: Class<*>
var telephonyEndCall: Method
var telephonyObject: Object
var serviceManagerObject: Object
telephonyClass = Class.forName(telephonyName);
telephonyStubClass = telephonyClass.getClasses()[0];
serviceManagerClass = Class.forName(serviceManagerName);
serviceManagerNativeClass = Class.forName(serviceManagerNativeName);
var getService = // getDefaults[29];
serviceManagerClass.getMethod("getService", String::class.java)
var tempInterfaceMethod = serviceManagerNativeClass.getMethod("asInterface", IBinder::class.java)
var tmpBinder = Binder()
tmpBinder.attachInterface(null, "fake");
serviceManagerObject = tempInterfaceMethod.invoke(null, tmpBinder) as Object;
var retbinder = getService.invoke(serviceManagerObject, "phone") as IBinder
var serviceMethod = telephonyStubClass.getMethod("asInterface", IBinder::class.java)
telephonyObject = serviceMethod.invoke(null, retbinder) as Object;
telephonyEndCall = telephonyClass.getMethod("endCall")
telephonyEndCall.invoke(telephonyObject)
} catch (e: InvocationTargetException) {
e.printStackTrace()
Log.d("unable", "msg cant dissconect call....")
}
但是类似的接受呼叫的方式无效。
try {
var telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
// Get the getITelephony() method
val classTelephony = Class.forName(telephonyManager.javaClass.name)
val method = classTelephony.getDeclaredMethod("getITelephony")
// Disable access check
method.isAccessible = true
// Invoke getITelephony() to get the ITelephony interface
val telephonyInterface = method.invoke(telephonyManager)
// Get the endCall method from ITelephony
val telephonyInterfaceClass = Class.forName(telephonyInterface.javaClass.name)
val methodEndCall = telephonyInterfaceClass.getMethod("answerRingingCall")
// Invoke endCall()
methodEndCall.invoke(telephonyInterface)
} catch (e: Exception) {
Log.e(TAG,"Unable to accept call")
e.printStackTrace()
}
有人可以告诉我在所有API级别中是否都有适当的方法来处理android中的接受,拒绝传入和断开传出呼叫吗?