如何在 Android 7.0 版本中显示弹出窗口 window

How to show popup window above call screen in Android 7.0 version

我正在为 android 用户开发一种联系人提供程序。主要功能是当有人呼叫 phone(锁定或非锁定)时,它会出现一个弹出窗口 window,其中包含有关呼叫者的一些信息。问题是 android 的版本低于 7.0 它运行正常,但在 android 7.0 及更高版本时,当手机被锁定并且有人调用 POPUPWINDOW 出现在呼叫屏幕布局下方并且仅在我挂了因此,如果有人可以帮助如何在 android 7.0 中使弹出窗口 window 出现在呼叫屏幕布局上方,我将非常感激。

PD。请记住,在 7.0 以下的版本中是 运行 所以问题出在新版本中。 7.0以下版本运行截图

public class IncomingCallActivity extends Activity {
int mCurrentX = 0;
int mCurrentY = 500;

@Override
protected void onCreate(Bundle savedInstanceState) {
    final LinearLayout fondo;
    TextView text;
    try {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_empty);

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
                WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

        LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);

        final View popupView = layoutInflater.inflate(R.layout.layout_incoming_call, null);

        Button button = (Button)popupView.findViewById(R.id.close_window);
        text = (TextView) popupView.findViewById(R.id.text);

        button.setText("CLOSE WINDOW");

        final PopupWindow popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.MATCH_PARENT, 300);
        new Handler().postDelayed(new Runnable() {
            public void run() {
                popupWindow.showAtLocation(popupView, Gravity.NO_GRAVITY,mCurrentX,mCurrentY);
                popupView.bringToFront();
            }
        }, 100);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                finish();
            }
        });

        popupView.setOnTouchListener(new View.OnTouchListener() {
            int orgX, orgY;
            int offsetX, offsetY;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        orgX = (int) (mCurrentX - event.getRawX());
                        orgY = (int) (mCurrentY - event.getRawY());
                        break;
                    case MotionEvent.ACTION_MOVE:
                        mCurrentX = (int) event.getRawX() + orgX;
                        mCurrentY = (int) event.getRawY() + orgY;
                        popupWindow.update(mCurrentX, mCurrentY, -1, -1, true);
                        break;
                }
                return true;
            }
        });

        String number = getIntent().getStringExtra(
                TelephonyManager.EXTRA_INCOMING_NUMBER);
        text.setText("Incoming call from " + number);

    } catch (Exception e) {
        Log.d("Exception", e.toString());
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

我找到了 API > 23 的解决方案。 我相信如果清单文件中有 android.permission.SYSTEM_ALERT_WINDOW,您也可以手动激活它,方法是转到设置 > 应用程序 > 你的应用程序 > 高级 - 绘制其他应用程序(如果你滚动向下)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(this)) {

    //If the draw over permission is not available open the settings screen
    //to grant the permission.
    Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
    Uri.parse("package:" + getPackageName()));
    startActivityForResult(intent, PERMISSION_DRAW_OVER_OTHER_APP);
} 

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PERMISSION_DRAW_OVER_OTHER_APP) {
        //Check if the permission is granted or not.
        if (resultCode == RESULT_OK) {
            // write your view here ...
        } else { //Permission is not available
            Toast.makeText(this,
                    "Draw over other app permission not available",
                    Toast.LENGTH_SHORT).show();
        }
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}