android接听来电 [英] android answer incoming call
问题描述
我拦截了来电并在标准屏幕上显示我的活动。
在这项活动中,我有接听电话和拒绝电话按钮,但我无法正常工作。
I have intercepted incoming call and show my activity over standard screen. On this activity I have buttons "answer call" and "reject call", but I can't do it working.
我找到了两个解决方案接听/拒绝电话programaticaly:
I have found two solutions to answer/reject phone call programaticaly:
- 使用ITelephony.aidl但它仅在api v10之前有效。所以这是错误的方式
-
以下内容:
- With ITelephony.aidl but it works only before api v10. So it is wrong way
With following:
private void acceptCall(Context context) {
Intent buttonDown = new Intent(Intent.ACTION_MEDIA_BUTTON);
buttonDown.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(
KeyEvent.ACTION_UP, 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");
}
private void acceptCall(Context context) {
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");
}
从这里 http://www.codeproject.com/Tips/578817/Reject - 并且接受来电
但是没有!我点击按钮并相应调用上面的方法,没有任何反应。
我在几台设备上进行了测试,结果相同。
However nothing! I click buttons and invoke methods above accordingly, and nothing happens. I tested it on several devices, and result the same.
嗯,然后我成功回答了三星nexus one,但没有回复HTC有意义。拒绝功能对两者都不起作用。
Hmm, then I was successful with answering on samsung nexus one, but not on HTC with sense. Reject function doesnt work on both.
推荐答案
结果我找到了 http://androidbridge.blogspot.com/2011/05/how-to-answer-incoming- call-in-android.html
这是适用于HTC设备的解决方案:
Here is solution which works on HTCs devices:
在Android 2.3.3 HTC Sensation中,这段代码不起作用。原因是在2.3.3我发现HeadsetObserver正在侦听实际的蓝牙插件事件。因此,您需要发送一个Intent假装已经连接了耳机。要解决此问题,您需要在调用上面的代码之前发送ACTION_HEADSET_PLUG Intent。
In Android 2.3.3 HTC Sensation this piece of code does not work. Reason is in 2.3.3 I found a HeadsetObserver listening for actual bluetooth plug-in event. So you need to send a Intent pretending there is a headset connected already. To fix this problem you need to send the ACTION_HEADSET_PLUG Intent before calling the above code.
所以我在接受调用代码之前添加了以下代码:
So I added following code before accept call code:
Intent headSetUnPluggedintent = new Intent(Intent.ACTION_HEADSET_PLUG);
headSetUnPluggedintent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
headSetUnPluggedintent.putExtra("state", 1); // 0 = unplugged 1 = Headset with microphone 2 = Headset without microphone
headSetUnPluggedintent.putExtra("name", "Headset");
// TODO: Should we require a permission?
sendOrderedBroadcast(headSetUnPluggedintent, null);
所以最终工作代码是
private void acceptCall() {
setHeadSetConnectEmulated();
Intent buttonUP = new Intent(Intent.ACTION_MEDIA_BUTTON);
buttonUP.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
IncomingCallActivity.this.getApplicationContext().sendOrderedBroadcast(buttonUP, "android.permission.CALL_PRIVILEGED");
}
private void setHeadSetConnectEmulated(){
Intent headSetUnPluggedintent = new Intent(Intent.ACTION_HEADSET_PLUG);
headSetUnPluggedintent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
headSetUnPluggedintent.putExtra("state", 1); // 0 = unplugged 1 = Headset with microphone 2 = Headset without microphone
headSetUnPluggedintent.putExtra("name", "Headset");
// TODO: Should we require a permission?
sendOrderedBroadcast(headSetUnPluggedintent, null);
}
private void rejectCall() {
Intent buttonDown = new Intent(Intent.ACTION_MEDIA_BUTTON);
buttonDown.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK));
IncomingCallActivity.this.getApplicationContext().sendOrderedBroadcast(buttonDown, "android.permission.CALL_PRIVILEGED");
}
这篇关于android接听来电的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!