如何在marshmallow及以上的android中以编程方式回答来电?(以上安全补丁更新日期1/09/16)

时间:2017-05-01 13:24:53

标签: android android-6.0-marshmallow android-7.0-nougat incoming-call

大家好我正在制作一个Android应用程序,通过我的应用程序回答所有来电 我已经为lolipop和以下做得很好,但也想为棉花糖及以上做到这一点,我做了很多R& D但是没有找到任何满意的解决方案,所以我问自己

以下代码不适用于棉花糖及以上版本。 这段代码一直工作到Lolipop。

  private void acceptCall_n() {

            // for HTC devices we need to broadcast a connected headset
            boolean broadcastConnected = MANUFACTURER_HTC.equalsIgnoreCase(Build.MANUFACTURER)
                    && !audioManager.isWiredHeadsetOn();



            if (broadcastConnected) {
                broadcastHeadsetConnected(false);
            }

            try {

                try {
                    // logger.debug("execute input keycode headset hook");
                    Runtime.getRuntime().exec("input keyevent " +
                            Integer.toString(KeyEvent.KEYCODE_HEADSETHOOK));


                } catch (IOException e) {

                    Log.e("Call Exception ",e.toString());
                    HelperMethods.showToastS(getBaseContext(),"Call Exception one "+e.toString());
                    // Runtime.exec(String) had an I/O problem, try to fall back
                    //    logger.debug("send keycode headset hook intents");
                    String enforcedPerm = "android.permission.CALL_PRIVILEGED";
                    Intent btnDown = new Intent(Intent.ACTION_MEDIA_BUTTON).putExtra(
                            Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN,
                                    KeyEvent.KEYCODE_HEADSETHOOK));
                    Intent btnUp = new Intent(Intent.ACTION_MEDIA_BUTTON).putExtra(
                            Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP,
                                    KeyEvent.KEYCODE_HEADSETHOOK));

                     sendOrderedBroadcast(btnDown, enforcedPerm);
                    sendOrderedBroadcast(btnUp, enforcedPerm);
                }
            }catch (Exception e){
                e.printStackTrace();
                Log.e("Call Exception two",e.toString());
                HelperMethods.showToastS(getBaseContext(),"Call Exception two "+e.toString());
            }finally {
                if (broadcastConnected) {
                    broadcastHeadsetConnected(false);
                }
            }
        }

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。它的根本原因是SELinux规则不允许执行命令。尝试使用this解决方案。我已在Android Marshmallow上测试过它。