如何在android中接听和断开呼叫

时间:2016-01-29 08:03:51

标签: android call

我的要求是接听和断开来电。

工作正常如果Call接受,然后等待3秒(handler.postDelayed)然后断开连接。

但我想在接受后立即断开电话。(不要等待3秒)

这是我的代码。感谢您的帮助。

public void acceptAndDissConnectCall(final Context activity) {

        int currentapiVersion = android.os.Build.VERSION.SDK_INT;
        if (currentapiVersion >= android.os.Build.VERSION_CODES.LOLLIPOP){
            Log.i(TAG, "It is LOLLIPOP or Greater====");
            answerCall(activity);
        } else{
            // do something for phones running an SDK before lollipop
            Log.i(TAG, "This device is prior to LOLLIPOP ====");
            acceptCall(activity);
        }
        dissConnectCall(activity);
    }

    public boolean dissConnectCall(final Context activity) {
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                try {
                    Log.d(TAG, "Dissconnect call ===== ");
                    // Get the boring old TelephonyManager
                    TelephonyManager telephonyManager = (TelephonyManager) activity.getSystemService(Context.TELEPHONY_SERVICE);

                    // Get the getITelephony() method
                    Class classTelephony = Class.forName(telephonyManager.getClass().getName());
                    Method methodGetITelephony = classTelephony.getDeclaredMethod("getITelephony");

                    // Ignore that the method is supposed to be private
                    methodGetITelephony.setAccessible(true);

                    // Invoke getITelephony() to get the ITelephony interface
                    Object telephonyInterface = methodGetITelephony.invoke(telephonyManager);

                    // Get the endCall method from ITelephony
                    Class telephonyInterfaceClass = Class.forName(telephonyInterface.getClass().getName());
                    Method methodEndCall = telephonyInterfaceClass.getDeclaredMethod("endCall");

                    // Invoke endCall()
                    methodEndCall.invoke(telephonyInterface);

                } catch (Exception ex) { // Many things can go wrong with reflection calls
                    ex.printStackTrace();
//                    return false;
                }
//                return true;
            }
        }, 4000);
        return false;

    }

    public static void acceptCall(Context context){
        Log.d(TAG, "accept the call ====");
        Intent buttonUp = new Intent(Intent.ACTION_MEDIA_BUTTON);
        buttonUp.putExtra(Intent.EXTRA_KEY_EVENT,
                new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
        context.sendOrderedBroadcast(buttonUp, "android.permission.CALL_PRIVILEGED");
    }

    public void answerCall(final Context mContext) {

        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    Log.d(TAG, "Answer the call ====");
                    Runtime.getRuntime().exec("input keyevent " + Integer.toString(KeyEvent.KEYCODE_HEADSETHOOK));
                } catch (IOException e) {
                    Log.e(TAG, "IOException on answerCall ========== ");
                    // Runtime.exec(String) had an I/O problem, try to fall back
                    String enforcedPerm = "android.permission.CALL_PRIVILEGED";
                    Intent btnDown = new Intent(Intent.ACTION_MEDIA_BUTTON).putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK));
                    Intent btnUp = new Intent(Intent.ACTION_MEDIA_BUTTON).putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));

                    mContext.sendOrderedBroadcast(btnDown, enforcedPerm);
                    mContext.sendOrderedBroadcast(btnUp, enforcedPerm);
                }
            }

        }).start();

    }

1 个答案:

答案 0 :(得分:0)

dissConnectCall方法中,将run方法中的值4000更改为最低值。

handler.postDelayed(new Runnable() {
            @Override
            public void run() {
          .......  
          }
        }, 4000);//decrease this value to 100