手机响铃时窗口覆盖

时间:2016-02-13 05:10:00

标签: android

我正试图在接听电话时显示一个类似于真正来电者的弹出对话框。我能够检测到用户何时使用广播接收器进行呼叫,并且我可以在电话状态开启时添加窗口。但如果手机被锁定,则会在呼叫者屏幕后面显示弹出对话框。

telephonyManager.listen(new PhoneStateListener() {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            super.onCallStateChanged(state, incomingNumber);
            switch (state) {
                case TelephonyManager.CALL_STATE_RINGING:
                    // CALL_STATE_RINGING
                    Log.d("MyReceiver", "I'm in " + state + " and the number is " + incomingNumber);
                    wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
                    params1 = new WindowManager.LayoutParams(
                            LayoutParams.MATCH_PARENT,
                            LayoutParams.MATCH_PARENT,
                            LayoutParams.TYPE_SYSTEM_ALERT |
                                    LayoutParams.TYPE_SYSTEM_OVERLAY,
                            LayoutParams.FLAG_NOT_TOUCH_MODAL |
                            LayoutParams.FLAG_NOT_FOCUSABLE |
                            LayoutParams.FLAG_TURN_SCREEN_ON |
                            LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                            LayoutParams.FLAG_KEEP_SCREEN_ON,
                            PixelFormat.TRANSPARENT);

                    params1.gravity = Gravity.TOP;
                    params1.height = 150;
                    params1.width = 512;
                    params1.x = 200;
                    params1.y = 200;
                    params1.format = PixelFormat.TRANSLUCENT;

                    ly1 = new LinearLayout(context);
                    ly1.setBackgroundColor(Color.BLACK);
                    ly1.setOrientation(LinearLayout.VERTICAL);

                    wm.addView(ly1, params1);
                    break;

                default:
                    break;
            }
        }
    }, PhoneStateListener.LISTEN_CALL_STATE);

该窗口不会出现在Android版Lollipop及以上版本的来电屏幕上。任何帮助将非常感激。谢谢。

1 个答案:

答案 0 :(得分:0)

我通过在显示窗口之前使用Keyguard管理器解锁屏幕解决了这个问题。

KeyguardManager km = (KeyguardManager) context .getSystemService(Context.KEYGUARD_SERVICE);
final KeyguardManager.KeyguardLock kl = km .newKeyguardLock("MyKeyguardLock");
kl.disableKeyguard();