以编程方式断开来电

时间:2014-08-04 08:29:09

标签: android

我想以编程方式断开传入呼叫,我搜索了这个,我在堆栈溢出时发现了很多链接。我尝试了所有这些,但没有一个工作

我在com.android.internal.telephony包中创建了ITelephony类

这是ITelephony类中的code

我使用的代码是......

public class Incommingcall extends BroadcastReceiver{
    TelephonyManager manager;
    int callstate;
    private ITelephony telephonyService;

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        try{
            manager = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);

            callstate = manager.getCallState();

        if(callstate == TelephonyManager.CALL_STATE_RINGING){
            Log.i("IncommingCall","Call is Ringing");
            Class c = Class.forName(manager.getClass().getName());
            Method m = c.getDeclaredMethod("getITelephony");
            Log.i("m= ", ""+m);
            m.setAccessible(true);
            telephonyService = (ITelephony) m.invoke(manager);

            telephonyService.endCall();
            Log.i("IncommingCall","Call Disconnected");
        }else if (callstate == TelephonyManager.CALL_STATE_IDLE) {

        }else if(callstate == TelephonyManager.CALL_STATE_OFFHOOK){

        }

        }catch(Exception e){

        }

    }

}

在清单文件中

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />

我得到的Logcat是......

08-04 13:57:48.686: I/IncommingCall(29302): Call is Ringing
08-04 13:57:48.686: I/m=(29302): private com.android.internal.telephony.ITelephony android.telephony.TelephonyManager.getITelephony()

2 个答案:

答案 0 :(得分:0)

您必须使用PhonestateListener,请检查以下方式

    private class MyPhoneStateListener extends PhoneStateListener {
    public void onCallStateChanged(int state, String incomingNumber) {


        switch (state) {
        case TelephonyManager.CALL_STATE_RINGING:
            // Disconnect the call here...
            try {
                TelephonyManager manager = (TelephonyManager) mContext
                        .getSystemService(Context.TELEPHONY_SERVICE);
                Class c = Class.forName(manager.getClass().getName());
                Method m = c.getDeclaredMethod("getITelephony");
                m.setAccessible(true);
                ITelephony telephony = (ITelephony) m.invoke(manager);
                telephony.endCall();


            } catch (Exception e) {
            }

            break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
            break;
        case TelephonyManager.CALL_STATE_IDLE:
            break;

        }
    }
}

按接收方式启动侦听器,如下所示

TelephonyManager tmgr;    
MyPhoneStateListener PhoneListener = new MyPhoneStateListener()
tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);

答案 1 :(得分:0)

我添加了这两个权限,现在工作正常

<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.PROCESS_INCOMING_CALLS" />