以编程方式接听电话无法正常工作

时间:2015-04-08 11:16:37

标签: android service phone-call

我有一个方法功能,在来电时激活,我点击了我的屏幕应用上的按钮。

下面你可以看到我的代码:

public void answerCall() throws Exception {

//        answerPhoneHeadsethook(mContext);


        TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
        @SuppressWarnings("rawtypes")
        Class c = Class.forName(tm.getClass().getName());
        @SuppressWarnings("unchecked")
        Method m = c.getDeclaredMethod("getITelephony");
        m.setAccessible(true);
        com.android.internal.telephony.ITelephony telephonyService = (com.android.internal.telephony.ITelephony) m.invoke(tm);
//      telephonyService.answerRingingCall();

        answerPhoneHeadsethookd(mContext);
        answerPhoneAidl(mContext);

    }

    private void answerPhoneAidl(Context context) throws Exception {
        // Set up communication with the telephony service (thanks to Tedd's Droid Tools!)
        TelephonyManager tm = (TelephonyManager) mContext.getSystemService(mContext.TELEPHONY_SERVICE);
        Class c = Class.forName(tm.getClass().getName());
        Method m = c.getDeclaredMethod("getITelephony");
        m.setAccessible(true);
        ITelephony telephonyService;
        telephonyService = (ITelephony)m.invoke(tm);

        // Silence the ringer and answer the call!
        telephonyService.silenceRinger();
        telephonyService.answerRingingCall();
} 
    private void answerPhoneHeadsethookd(Context context) {
        // Simulate a press of the headset button to pick up the call
        Intent buttonDown = new Intent(Intent.ACTION_MEDIA_BUTTON);             
        buttonDown.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK));
        context.sendOrderedBroadcast(buttonDown, "android.permission.CALL_PRIVILEGED");

        // froyo and beyond trigger on buttonUp instead of buttonDown
        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");
} 

不幸的是这个功能不起作用。

1 个答案:

答案 0 :(得分:0)

谷歌并不希望你这样做,而且它有道理,可能会在很多方面被滥用......

如果你想尝试一些"非法"方式,看看这里: How to programmatically answer/end a call in Android 4.1?