在Android中接收来电

在Android中接收来电,android,android-5.0-lollipop,android-5.1.1-lollipop,Android,Android 5.0 Lollipop,Android 5.1.1 Lollipop,我已经用android设计了一个呼叫屏幕。我已经尝试了下面提供的两种代码来接收来电,但它仅在棒棒糖设备中不起作用。在一些棒棒糖设备中,不是接收,而是断开来电 1) private void answerPhoneHeadsethook(Context context) { // Simulate a press of the headset button to pick up the call Intent buttonDown = new Intent(Intent.A

我已经用android设计了一个呼叫屏幕。我已经尝试了下面提供的两种代码来接收来电,但它仅在棒棒糖设备中不起作用。在一些棒棒糖设备中,不是接收,而是断开来电

1)

 private void answerPhoneHeadsethook(Context context) {
     // Simulate a press of the headset button to pick up the call
     Intent buttonDown = new Intent(Intent.ACTION_MEDIA_BUTTON);             
     buttonDown.putExtra(Intent.EXTRA_KEY_EVENT, new     KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK));
     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_HEADSETHOOK));
     context.sendOrderedBroadcast(buttonUp, "android.permission.CALL_PRIVILEGED");
 }


2)

new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        Runtime.getRuntime().exec("input keyevent " +
                                Integer.toString(KeyEvent.KEYCODE_HEADSETHOOK));
                    } catch (IOException e) {
                        // Runtime.exec(String) had an I/O problem, try to fall back
                        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));

                        ctx.sendOrderedBroadcast(btnDown, enforcedPerm);
                        ctx.sendOrderedBroadcast(btnUp, enforcedPerm);
                    }
                }

            }).start();