2

大家好,我正在制作一个 android 应用程序来通过我的应用程序接听所有来电所以我自己问

下面的代码不适用于棉花糖及以上。这段代码一直有效到 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);
                }
            }
        }
4

1 回答 1

0

我遇到了同样的问题。根本原因是 SELinux 规则不允许执行命令。尝试使用解决方案。我已经在 Android Marshmallow 上对其进行了测试,它可以工作。

于 2017-05-22T15:57:01.303 回答