Android 安卓4.0.3中的来电自动应答

Android 安卓4.0.3中的来电自动应答,android,Android,我在安卓技术公司工作了1年。目前,我想在Android 4.0.3中开发一个应用程序来电自动应答,但在这个版本中,我没有得到任何解决方案,也找不到任何api(ITelephony.aidl)。请建议我。为了接听或拒绝电话,需要修改电话状态许可。不幸的是,自2.3及以后,它仅适用于系统应用程序。(更多信息) 接听电话的变通方法(最初来自): 它的工作代码。 首先用手机状态广播接收机找出其来电 filter.addAction("android.intent.action.PHONE_STA

我在安卓技术公司工作了1年。目前,我想在Android 4.0.3中开发一个应用程序来电自动应答,但在这个版本中,我没有得到任何解决方案,也找不到任何api(ITelephony.aidl)。请建议我。

为了接听或拒绝电话,需要修改电话状态许可。不幸的是,自2.3及以后,它仅适用于系统应用程序。(更多信息)

接听电话的变通方法(最初来自):

它的工作代码。 首先用手机状态广播接收机找出其来电

    filter.addAction("android.intent.action.PHONE_STATE");
    mContext.registerReceiver(myCallReceiver, filter);
然后在onReceive(上下文,意图)中调用answerPhoneHeadsethook()函数

private void answerPhoneHeadsethook(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");
}

从安卓2.2到4.0都可以使用,现在在最后一行添加了try-catch之后,4.1.2和4.2都可以使用,坦白地说,我不知道它是如何工作的,但它对我有效

这对我在安卓4.1.2中起作用,我也在4.2上测试过
这仍然会产生一个已处理的异常。

请您解释一下这里的逻辑。根据谷歌的说法,KEYCODE_Headshook会挂断电话。我已经尝试了几种方法,但没有得到任何结果,即使我可以通过adb(adb shell input keyevent 5)发送事件、KEYCODE_调用,并且它工作得非常好。我需要在代码中的这个。有什么想法吗?上面的代码示例对您无效吗?此代码段模拟按下耳机按钮,如同耳机已连接。通常,当您单击此按钮时,它会接听电话和/或结束已激活的电话。由于这是一种解决方法,因此它不会在所有设备上100%的时间工作。这取决于原始设备制造商的内部实施。请看我的问题:这个问题解决了吗?请跟进。操作\u耳机\u插头的安全例外,不允许非系统应用程序使用:(正确,但您可以在清单中输入权限,尽管您会收到一个错误,它正在工作。您可以从www.virtualmodelz.com下载该应用程序,该应用程序工作正常。不适用于Android 4.2.2:(.尝试添加CALL_PRIVEGED权限。权限在android 4.0+设备上不起作用您需要应用程序的系统权限才能授予此类权限,但这对my有效,可能只是一个bug…不确定,但很难解释。
private void answerPhoneHeadsethook(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");
}
          Log.d(tag, "InSecond Method Ans Call");
    // 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));
    sendOrderedBroadcast(buttonUp, "android.permission.CALL_PRIVILEGED");
    Intent headSetUnPluggedintent = new Intent(Intent.ACTION_HEADSET_PLUG);
    headSetUnPluggedintent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
    headSetUnPluggedintent.putExtra("state", 0);
    headSetUnPluggedintent.putExtra("name", "Headset");
    try {
        sendOrderedBroadcast(headSetUnPluggedintent, null);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }