Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何以编程方式接听电话?_Android_Android Intent - Fatal编程技术网

Android 如何以编程方式接听电话?

Android 如何以编程方式接听电话?,android,android-intent,Android,Android Intent,我想接一个电话。我找到了intentandroid.intent.action.ANSWER,但似乎我得到的唯一效果是ActivityNotFoundException。为什么?这是一个不赞成的意图吗?我怎样才能得到答案?我也听说过“telnet技术”。那是什么 谢谢这是不可能的,请查看线程以了解更多信息。此事件不起作用。 你应使用: Intent i = new Intent(Intent.ACTION_MEDIA_BUTTON); KeyEvent event = new KeyEvent(

我想接一个电话。我找到了intent
android.intent.action.ANSWER
,但似乎我得到的唯一效果是ActivityNotFoundException。为什么?这是一个不赞成的意图吗?我怎样才能得到答案?我也听说过“telnet技术”。那是什么

谢谢

这是不可能的,请查看线程以了解更多信息。

此事件不起作用。 你应使用:

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

模拟按键操作。

接听来电是可能的。检查此项:

应答意图应发送到InCallScreen

adb shell am start -n com.android.phone/.InCallScreen -a android.intent.action.ANSWER

您还可以发送call keyevent来接听电话 但该设备需要扎根

接听电话:

try {
    Thread.sleep(800); 
    Process process = Runtime.getRuntime().exec(new String[]{ "su","-c","input keyevent 5"});
    process.waitFor();

}catch (Exception e) {
    e.printStackTrace();
}
结束通话:

try {
    Thread.sleep(800); 
    Process process = Runtime.getRuntime().exec(new String[]{ "su","-c","input keyevent 6"});
    process.waitFor();

}catch (Exception e) {
    e.printStackTrace();
}

模拟BT手机所使用的方法并不适用于所有Android版本,也不适用于所有设备,因为BT手机的处理方式可能不同

在许多设备和Android版本中验证的最佳方法包括模拟拨号器中呼叫按钮的压力和释放:

Intent buttonDown = new Intent(Intent.ACTION_MEDIA_BUTTON);
buttonDown.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_CALL));
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_CALL));
context.sendOrderedBroadcast(buttonUp, "android.permission.CALL_PRIVILEGED"); 

您也可以通过adb或Runtime.exec发送Shell命令“input keyevent 5”来实现这一点,但在我的例子中,它并不适用于所有设备,从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();
    }
这对我在安卓4.1.2中起作用,我也在4.2上测试过
这仍然会给出一个已处理的异常。

好的,看到了线程…但为什么会有这样的意图?而且它甚至没有被弃用!我知道,如果你在谷歌上搜索ACTION_ANSWER,你会发现很多关于人们为了找到自动接听电话的方法而发疯的话题。这项功能肯定已经被安卓员工关闭了(安全原因?)。它真的有效吗?我还没有试过。是吗?ThanksIt可以工作,但随后扬声器手机会静音。你知道怎么预防吗。我在AudioManager类中尝试了setMicrophoneMute(false),但没有结果??在棒棒糖中并不总是有效。尝试从服务中使用它,但从屏幕关闭状态(当手机处于空闲状态时拨打电话)时将不起作用。这实际上是在使用黑客攻击,在Android的下一次更新中可能会起作用,也可能不会起作用。在安卓N上不再可能了。看看日期。这是5年前的事了:-)
try {
Runtime.getRuntime().exec("input keyevent"+Integer.toString(KeyEvent.KEYCODE_HEADSETHOOK));
} catch (IOException e) {
   //handle error here
}