Display dialog from Input Method Service in Android 9 (Android Pie)

本小妞迷上赌 提交于 2019-12-11 18:06:50

问题


My app includes an Input Method Service with a special button that brings up a dialog. For users with Android 9, this dialog is not displayed correctly, only the part above the IME is visible:

The code to create the dialog is

    AlertDialog dialog = builder.create();
    Window window = dialog.getWindow();
    WindowManager.LayoutParams lp = window.getAttributes();
    LatinKeyboardView inputView = mKeyboardSwitcher.getInputView();
    lp.token = inputView.getWindowToken();
    lp.type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
    window.setAttributes(lp);
    window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

    dialog.show();

which is the same as described in https://stackoverflow.com/a/13962770/292233

I also tried TYPE_APPLICATION_PANEL as suggested in https://stackoverflow.com/a/3508462/292233 but this doesn't help either.

Is there any easy fix to this?


回答1:


The problem can be fixed by using WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG instead of TYPE_APPLICATION_ATTACHED_DIALOG and requesting android.permission.SYSTEM_ALERT_WINDOW.

Detailed change is here: https://github.com/osfans/trime/commit/d8e9da2dfe2653c94cd4aecba00728b7a910cbb8




回答2:


If both PopupWindow & AlertDialog do not work, I think we can use floating view, like this:

  1. Custom a view, make it look like a dialog.
  2. Use WindowManager to add the view, the view will be shown on top of any android application. I have learnt it from FlowtingExample

I tried using it. It works on Android P. However, this method get serious problem with global keys:

When "the dialog" is showing from keyboard, user press global key like Home key.

=> Home screen is shown. But, "the dialog" & keyboard is still showing.

(Is there anyway to fix this limitation?).

(I heard language selection dialog from Google keyboard (Gboard) works perfectly on Android P. But I cant find source code of Gboard. Is there anyway to get it?)

Sorry I am totally new with stackoverflow, I think I should post this into comment section but I cant do that because I do not have enough 50 reputations.



来源:https://stackoverflow.com/questions/51906586/display-dialog-from-input-method-service-in-android-9-android-pie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!