【问题标题】:How to make AlertDialog view in Input method Service?如何在输入法服务中制作 AlertDialog 视图?
【发布时间】:2015-12-10 01:29:48
【问题描述】:

我想做一个只用于软键盘的输入法。我如何在输入法中制作弹出onkey事件。

我正在创建对话框,但是你看到我的 logcat 有问题:

09-14 11:16:54.349: E/MessageQueue-JNI(7172):   at android.inputmethodservice.KeyboardView.detectAndSendKey(KeyboardView.java:824)

Softkeyboard.java

这里是java代码

public void onKey(int primaryCode, int[] keyCodes) {
        if (primaryCode == -2) {

            // add this to your code
            dialog = builder.create();
            Window window = dialog.getWindow();
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.token = mInputView.getWindowToken();
            lp.type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
            window.setAttributes(lp);
            window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
            // end addons
            builder.show();
        }

感谢您的提前..

【问题讨论】:

    标签: java android input android-softkeyboard keyboard-events


    【解决方案1】:

    您需要拥有ACTION_MANAGE_OVERLAY_PERMISSION权限才能在输入法中打开/显示Alert onkey事件。

    在设置自定义键盘之前检查覆盖权限。

     final boolean overlayEnabled = Settings.canDrawOverlays(MainActivity.this);
    
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !overlayEnabled) {
       openOverlaySettings();
     }
    
    
    
     @TargetApi(Build.VERSION_CODES.M)
        private void openOverlaySettings() {
            final Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                    Uri.parse("package:" + getPackageName()));
            try {
                startActivityForResult(intent, RC_OVERLAY);
            } catch (ActivityNotFoundException e) {
                Log.e("MainActivity", e.getMessage());
            }
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            switch (requestCode) {
                case RC_OVERLAY:
                    final boolean overlayEnabled = Settings.canDrawOverlays(this);
                    if (overlayEnabled) {
                        preferenceManager.setBooleanPref(IS_CYBER_BULLING_ON, true);
                        Intent intent = new Intent(MainActivity.this, ImePreferences.class);
                        startActivity(intent);
                        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                    } else {
    //                    switchCyberBulling.setChecked(false);
                    }
                    // Do something...
                    break;
            }
        }
    

    然后在您的 SoftKeyboard.java 类中,放置警报对话框的代码并将警报类型设置为“TYPE_APPLICATION_OVERLAY”。

    AlertDialog.Builder builder = new AlertDialog.Builder(this)
                        //set icon
                        .setIcon(android.R.drawable.ic_dialog_alert)
    
                        //set title
                        .setTitle("Warning!")
                        //set message
                        .setMessage("This is alert dialog!")
                        //set positive button
                        .setPositiveButton("Okay", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                //set what would happen when positive button is clicked
                                dialogInterface.dismiss();
                            }
                        })
                        //set negative button
                        .setNegativeButton("Dismiss", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                //set what should happen when negative button is clicked
                                dialogInterface.dismiss();
                            }
                        });
                AlertDialog alertDialog = builder.create();
    
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
                }else{
                    alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
                }
                alertDialog.show();
    

    不要忘记绘制叠加权限。希望这对您有所帮助。 :)

    【讨论】:

    • @Sonu Kumar,很高兴这个答案对你有帮助。
    • 我们能否在应用关闭时显示前台服务的弹出窗口??
    • @PradeepkumarReddy 是的,它也在处理这种情况
    • 没有。警报对话框无法在前台服务中工作。我收到以下异常“android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your Activity running?”
    • 这是因为您没有检查覆盖权限。按照所有步骤仔细阅读,我在步骤 1 中提到了所有内容。
    猜你喜欢
    • 2014-01-12
    • 2019-05-19
    • 1970-01-01
    • 2018-04-23
    • 1970-01-01
    • 2022-07-15
    • 2013-10-06
    • 1970-01-01
    • 2011-10-23
    相关资源
    最近更新 更多