简体   繁体   English

如何在棉花糖及更高版本的Android中以编程方式接听来电?(以上安全补丁更新日期为1/09/16)

[英]How to answer incoming call programmatically in android in marshmallow and above?(Above Security patch update date 1/09/16 )

Hi Everyone i am making a android app to answer all incoming call through my app i have done nicely for lolipop and below but also want to do that for marshmallow and above , i did lot of R & D but have not found any satisfied solution , so i am asking by my own 嗨,大家好,我正在制作一个Android应用程序,以通过我的应用程序应答所有来电,我对lolipop及以下产品做得很好,但也对棉花糖及以上产品做得很好,我做了很多研发工作,但没有找到满意的解决方案,所以我自己问

Below code is not working for marshmallow and above. 以下代码不适用于棉花糖及更高版本。 This code is working till Lolipop. 该代码一直有效到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);
                }
            }
        }

I faced the same issue. 我遇到了同样的问题。 Root cause of it is that executing command is not allowed by SELinux rules. 根本原因是SELinux规则不允许执行命令。 Try to use this solution. 尝试使用解决方案。 I've tested it on Android Marshmallow it works. 我已经在Android Marshmallow上测试了它的工作原理。

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

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