Android 以编程方式结束来电
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20965702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
end incoming call programmatically
提问by RuntimeException
This is a familiar question in SO. and what I need is to end call programmatically. I have searched a lot...
这是SO中的一个熟悉的问题。我需要的是以编程方式结束通话。我搜索了很多...
http://androidsourcecode.blogspot.in/2010/10/blocking-incoming-call-android.htmlhttp://androiddesk.wordpress.com/2012/08/02/blocking-a-call-without-user-intervention-in-android/
http://androidsourcecode.blogspot.in/2010/10/blocking-incoming-call-android.html http://androiddesk.wordpress.com/2012/08/02/blocking-a-call-without-user-intervention -in-android/
Rejecting Incoming call in android
How to programmatically answer/end a call in Android 4.1?
如何在 Android 4.1 中以编程方式接听/结束通话?
http://www.emoticode.net/android-sdk/block-incoming-and-outgoing-phone-calls-programmatically.html
http://www.emoticcode.net/android-sdk/block-incoming-and-outgoing-phone-calls-programmatically.html
how to block a mobile number call and message receiving in android application development?
如何在android应用程序开发中阻止手机号码呼叫和消息接收?
and http://androidsourcecode.blogspot.in/2010/10/blocking-incoming-call-android.html
和http://androidsourcecode.blogspot.in/2010/10/blocking-incoming-call-android.html
and lot more questions, answers and suggestions...
还有更多的问题、答案和建议……
All are saying use ITelephonyService.aidl
in combination with TelephonyManager
都说ITelephonyService.aidl
与TelephonyManager结合使用
and the solution is working perfect on many devices but it's not working on Samsung S Duos. I am struggling over a week but didn't get a solution.. is there any special API to work with on this type of devices? How can I reject incoming call? please help me...
并且该解决方案在许多设备上都能完美运行,但不适用于Samsung S Duos。我挣扎了一个多星期,但没有得到解决方案.. 是否有任何特殊的 API 可用于此类设备?如何拒绝来电?请帮我...
回答by Jebasuthan
Try this Sure it will work. It's working fine for me.
试试这个肯定会起作用。它对我来说很好用。
You could download the ITelephony.java file from ITelephony.java
您可以从ITelephony.java下载 ITelephony.java 文件
After that you add the method to end call:
之后,您添加方法以结束调用:
Function to Disconnect call
断开呼叫的功能
public void disconnectCall(){
try {
String serviceManagerName = "android.os.ServiceManager";
String serviceManagerNativeName = "android.os.ServiceManagerNative";
String telephonyName = "com.android.internal.telephony.ITelephony";
Class<?> telephonyClass;
Class<?> telephonyStubClass;
Class<?> serviceManagerClass;
Class<?> serviceManagerNativeClass;
Method telephonyEndCall;
Object telephonyObject;
Object serviceManagerObject;
telephonyClass = Class.forName(telephonyName);
telephonyStubClass = telephonyClass.getClasses()[0];
serviceManagerClass = Class.forName(serviceManagerName);
serviceManagerNativeClass = Class.forName(serviceManagerNativeName);
Method getService = // getDefaults[29];
serviceManagerClass.getMethod("getService", String.class);
Method tempInterfaceMethod = serviceManagerNativeClass.getMethod("asInterface", IBinder.class);
Binder tmpBinder = new Binder();
tmpBinder.attachInterface(null, "fake");
serviceManagerObject = tempInterfaceMethod.invoke(null, tmpBinder);
IBinder retbinder = (IBinder) getService.invoke(serviceManagerObject, "phone");
Method serviceMethod = telephonyStubClass.getMethod("asInterface", IBinder.class);
telephonyObject = serviceMethod.invoke(null, retbinder);
telephonyEndCall = telephonyClass.getMethod("endCall");
telephonyEndCall.invoke(telephonyObject);
} catch (Exception e) {
e.printStackTrace();
Log.error(DialerActivity.this,
"FATAL ERROR: could not connect to telephony subsystem");
Log.error(DialerActivity.this, "Exception object: " + e);
}
}
回答by Alejandro Martínez Martínez
Try this function:
试试这个功能:
private void endCall() {
Context context = getApplicationContext();
try {
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
Class<?> c = Class.forName(telephony.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
ITelephony telephonyService = (ITelephony) m.invoke(telephony);
// Funciona en 2.2
// Funciona en 2.3.3
telephonyService.endCall();
logManager.debug("ITelepony was used (endCall)");
} catch (Exception e) {
logManager.error("Error ending call: " + e.getMessage());
logManager.debug("Error ending call", e);
}
}
回答by Ramil Mammadov
In your Application create a package (com.android.internal.telephony) in java folder. And then copy ITelephone interface in that package
在您的应用程序中,在 java 文件夹中创建一个包(com.android.internal.telephony)。然后复制该包中的 ITelephone 接口
ITelephone interface source: http://grepcode.com/file_/repository.grepcode.com/java/ext/com.google.android/android/2.1_r2/com/android/internal/telephony/ITelephony.java/?v=source
ITelephone接口源码:http://grepcode.com/file_/repository.grepcode.com/java/ext/com.google.android/android/2.1_r2/com/android/internal/telephony/ITelephony.java/?v= 来源
Code for Disconnect Call:
断开呼叫代码:
Context context = getApplicationContext();
try {
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
Class<?> c = Class.forName(telephony.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
ITelephony telephonyService = (ITelephony) m.invoke(telephony);
telephonyService.answerRingingCall();
} catch (Exception e) {
}
Also add permissions for using this methods:
还要添加使用此方法的权限:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.PROCESS_INCOMING_CALLS" />