简体   繁体   English

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

[英]How to programmatically answer a call in Android 4.0.3?

So as the subject states I need to be able to answer a phone call programmatically in Android 4.0.3 on an HTC OneX. 因此,正如受试者所说,我需要能够在HTC OneX上的Android 4.0.3中以编程方式接听电话。 I have read several places that the MODIFY_PHONE_STATE permission has been revoked by Google so to do this task you need a work around. 我已经读过几个地方, MODIFY_PHONE_STATE权限已被Google吊销,因此要执行此任务,您需要解决。

I have looked into two avenues so far: 到目前为止,我已经研究了两种方法:

(1) Following Guy's post here and using a BroadcastReceiver (1)按照Guy 在这里的帖子并使用BroadcastReceiver

(2) Using the following code to try and hit a key event through a shell command. (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();
    } 

I use this because keyevent 5 is KeyEvent.CALL according to Google and it works in adb using 我用这个是因为keyevent 5是Google所说的KeyEvent.CALL,它可以在adb中使用

adb shell input keyevent 5

My question is, what am I doing wrong? 我的问题是,我在做什么错? Because logically both of these methods makes sense but neither are working or even generating runtime errors of any kind. 因为从逻辑上讲,这两种方法都有意义,但它们都不起作用,甚至不会产生任何类型的运行时错误。

Cheers 干杯

After days of research, I found that using both a broadcast receiver route and a runtime.exec() route it is simply not possible to answer a phone call in Android 4.0.3 using the Android API. 经过数天的研究,我发现同时使用广播接收器路由和runtime.exec()路由根本无法使用Android API在Android 4.0.3中接听电话。

For those of you still wondering, I did find some useful information...You CAN answer a call through adb using the command adb shell input keyevent 5 5 is the key code for the call button and in Android it is the KEYEVENT_CALL 对于那些仍然想知道的人,我确实找到了一些有用的信息...您可以使用命令adb shell input keyevent 5通过adb接听电话adb shell input keyevent 5 5是呼叫按钮的键代码,在Android中是KEYEVENT_CALL

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上进行了测试都适用。这仍然给出了一个已处理的异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM