通过按钮结束呼叫

时间:2014-04-14 11:02:06

标签: android call

我正试图通过按钮结束通话。通话结束仅适用于少数几部手机。如何在所有手机中使用它?

在几部电话上,电话根本没有被切断?

以下是我的尝试:

点击END CALL,调用以下内容:

 TelephonyManager telephony = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);

    try 
    {
        // Java reflection to gain access to TelephonyManager's
        // ITelephony getter
        Class c = Class.forName(telephony.getClass().getName());
        Method m = c.getDeclaredMethod("getITelephony");
        m.setAccessible(true);
        com.android.internal.telephony.ITelephony telephonyService = (ITelephony) m.invoke(telephony);
        telephonyService.endCall();
        finish();

        timeSwapBuff += timeInMilliseconds;
        customHandler.removeCallbacks(updateTimerThread);
    }
    catch (Exception e) 
    {
        e.printStackTrace();
        Log.e("Error", "FATAL ERROR: could not connect to telephony subsystem");
        Log.e("Error", "Exception object: " + e);
    }

让我知道如何解决这个问题?

谢谢!

2 个答案:

答案 0 :(得分:0)

/**
 * Reject button click listener will reject the incoming call.
 */
private class RejectCallOnClickListener implements OnClickListener {
    @Override
    public void onClick(View v) {
        Log.d(tag, "OnRejectButton: " + "Reject OnClick");
        ignoreCall();
        exitCleanly();
    }
}

/**
 * ignore incoming calls
 */
private void ignoreCall() {
    if (USE_ITELEPHONY)
        ignoreCallAidl();
    else
        ignoreCallPackageRestart();
}
/**
 * AIDL/ITelephony technique for ignoring calls
 */
private void ignoreCallAidl() {
    try {
        // telephonyService.silenceRinger();

        telephonyService.endCall();
    } catch (RemoteException e) {
        e.printStackTrace();
        Log.d(tag, "ignoreCall: " + "Error: " + e.getMessage());

    } catch (Exception e) {
        e.printStackTrace();
        Log.d(tag, "ignoreCall" + "Error: " + e.getMessage());

    }
}
/**
 * package restart technique for ignoring calls
 */
private void ignoreCallPackageRestart() {
    ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    am.restartPackage("com.android.providers.telephony");
    am.restartPackage("com.android.phone");
}
/**
 * cleanup and exit routine
 */
private void exitCleanly() {
    unHookReceiver();
    this.finish();

}

答案 1 :(得分:0)

在iTelephony的界面上下载iTelephony make

并使用此代码

            TelephonyManager tm = (TelephonyManager) CallActivity.this.getSystemService(Context.TELEPHONY_SERVICE);
            Class c = null;
            try {
                c = Class.forName(tm.getClass().getName());
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            Method m = null;
            try {
                m = c.getDeclaredMethod("getITelephony");
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
            m.setAccessible(true);
            Object telephonyService = null; // Get the internal ITelephony object
            try {
                telephonyService = m.invoke(tm);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            try {
                c = Class.forName(telephonyService.getClass().getName()); // Get its class
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            try {
                m = c.getDeclaredMethod("endCall"); // Get the "endCall()" method
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
            m.setAccessible(true); // Make it accessible
            try {
                m.invoke(telephonyService); // invoke endCall()
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }