Android 如何以编程方式接听电话?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2610587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to programmatically answer a call?
提问by Matroska
I want to answer a phone call. I found the intent android.intent.action.ANSWER
but it seems that the only effect that I obtain is an ActivityNotFoundException. Why? Is it a deprecated intent? How can I achieve answer? I have also heard about the "telnet technique". What is that?
我想接电话。我找到了意图,android.intent.action.ANSWER
但似乎我获得的唯一效果是 ActivityNotFoundException。为什么?这是一个已弃用的意图吗?我怎样才能得到答案?我也听说过“telnet 技术”。那是什么?
Thanks
谢谢
采纳答案by systempuntoout
回答by Seifolahi
you can also send call keyevent to answer calls but the device needs to be rooted
您也可以发送 call keyevent 来接听电话,但设备需要植根
answer calls :
接听电话:
try {
Thread.sleep(800);
Process process = Runtime.getRuntime().exec(new String[]{ "su","-c","input keyevent 5"});
process.waitFor();
}catch (Exception e) {
e.printStackTrace();
}
End calls :
结束通话:
try {
Thread.sleep(800);
Process process = Runtime.getRuntime().exec(new String[]{ "su","-c","input keyevent 6"});
process.waitFor();
}catch (Exception e) {
e.printStackTrace();
}
回答by Gautam Naroji
回答by midoub
this event dose not work. you should use:
此事件不起作用。你应该使用:
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);
that emulate a bhavior of press key.
模拟按键的行为。
回答by PravinDodia
This works from Android 2.2 to 4.0 and now after adding the try catch to the last line it works for 4.1.2 and 4.2 Frankly speaking dont know how it works but it works for me.
这适用于 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();
}
This is working for me in Android 4.1.2 as well as i have tested on 4.2 This still gives an exception which is handled.
这在 Android 4.1.2 中对我有用,并且我已经在 4.2 上测试过这仍然给出了一个处理的异常。
回答by stefanogreg
The approach used by simulating a BT handset does not work with all Android Versions and with all Devices as BT handset handling may be different.
模拟 BT 手机使用的方法不适用于所有 Android 版本和所有设备,因为 BT 手机处理可能不同。
The best approach verified in lot of devices and Android versions consists in emulating the pressure and release of the Call button in the Dialer:
在许多设备和 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");
You can do that also by sending Shell command "input keyevent 5" via adb or via Runtime.exec but in my case wasn't working for all devices
您也可以通过 adb 或 Runtime.exec 发送 Shell 命令“input keyevent 5”来做到这一点,但在我的情况下不适用于所有设备
回答by Jana
try {
Runtime.getRuntime().exec("input keyevent"+Integer.toString(KeyEvent.KEYCODE_HEADSETHOOK));
} catch (IOException e) {
//handle error here
}
回答by Rookie
Answer intent should be sent to the InCallScreen.
应将应答意图发送到 InCallScreen。
adb shell am start -n com.android.phone/.InCallScreen -a android.intent.action.ANSWER