Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
725 views
in Technique[技术] by (71.8m points)

java - How to make AlertDialog view in Input method Service?

I would like to make an input method which is used only for SoftKeyboard. My how to make popup onkey event in input method.

I am creating Dialog but here is problem you see my logcat:

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

Softkeyboard.java

Here java code

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();
        }

Thanks for advance..

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to have ACTION_MANAGE_OVERLAY_PERMISSION permission to open/display Alert onkey event in input method.

Before you set your custom Keyboard Check for Overlay Permission.

 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;
        }
    }

Then inside your SoftKeyboard.java class, put code for alert dialog & set alert type of "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();

Do not forget for Draw Overlay Permission. Hope this helps you. :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...