How to programmatically answer a call?
I want to answer a phone call. I found the intent android.intent.action.ANSWER
but it seems that the only effect tha开发者_StackOverflow社区t 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?
Thanks
you can also send call keyevent to answer calls but the device needs to be rooted
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();
}
It's not possible, check this thread for further information.
It is possible to answer an Incoming Phone Call. Check this out : here
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.
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.
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.
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.
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:
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
Answer intent should be sent to the InCallScreen.
adb shell am start -n com.android.phone/.InCallScreen -a android.intent.action.ANSWER
try {
Runtime.getRuntime().exec("input keyevent"+Integer.toString(KeyEvent.KEYCODE_HEADSETHOOK));
} catch (IOException e) {
//handle error here
}
精彩评论