android 4.0.3中的来电自动回答

时间:2012-07-25 11:22:43

标签: android

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

3 个答案:

答案 0 :(得分:2)

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

    filter.addAction("android.intent.action.PHONE_STATE");
    mContext.registerReceiver(myCallReceiver, filter);

然后在onReceive(Context context,Intent intent)中调用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");
}

答案 1 :(得分:0)

为了接听或拒绝电话,需要MODIFY_PHONE_STATE权限。不幸的是,从2.3及以后它只适用于系统应用程序。 (更多信息here

接听电话的解决方法(最初来自here):

Intent i = new Intent(Intent.ACTION_MEDIA_BUTTON);
KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_HEADSETHOOK);
i.putExtra(Intent.EXTRA_KEY_EVENT, event );
context.sendOrderedBroadcast(i, null);         

答案 2 :(得分:0)

这适用于Android 2.2到4.0,现在将try catch添加到最后一行后,它适用于4.1.2和4.2坦率地说不知道它是如何工作的,但它对我有用。

          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();
    }

这对我在Android 4.1.2中工作以及我在4.2上测试过 这仍然是一个处理的例外。

相关问题