因此,正如主题所述,我需要能够在HTC OneX上的Android 4.0.3中以编程方式接听电话。我已经读过几个地方,MODIFY_PHONE_STATE
权限已被Google撤销,因此要执行此任务,您需要解决此问题。
到目前为止,我已经研究了两种途径:
(1)在这里关注盖伊的帖子并使用广播接收器
(2) 使用以下代码尝试通过 shell 命令命中键事件。
final Runtime r = Runtime.getRuntime();
try {
Process process = r.exec("input keyevent 5");
InputStream stream = process.getErrorStream();
log.v("Process Error Stream: " +stream.toString());
log.v("Sending shell command to Answer Call");
} catch (Exception e) {
log.v("Stack Trace: " + e.getStackTrace().toString());
e.printStackTrace();
}
我使用它是因为keyevent 5是KeyEvent.CALL根据谷歌,它在adb中使用
adb shell input keyevent 5
我的问题是,我做错了什么?因为从逻辑上讲,这两种方法都是有意义的,但两者都不起作用,甚至都没有生成任何类型的运行时错误。
干杯
经过几天的研究,我发现同时使用广播接收机路由和runtime.exec()
路由,根本不可能使用 Android API 在 Android 4.0.3 中接听电话。
对于那些仍然想知道的人,我确实发现了一些有用的信息......您可以使用以下命令通过adb
接听电话adb shell input keyevent 5
5 是呼叫按钮的键代码,在 Android 中它是KEYEVENT_CALL
这适用于 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 上进行了测试这仍然会给出一个已处理的异常。
这里有几个有用的链接,请检查它们:
- 自动接听来电
- 如何以编程方式自动应答呼叫
- 自动应答