Back pressed events with system alert window

后端 未结 10 1422
南笙
南笙 2021-01-03 03:21

I need to dismiss system alert window on back pressed and home button event.I have tried with onKeyEvent but in vain. As we can\'t cap

相关标签:
10条回答
  • 2021-01-03 03:48
    @Override
    public void onBackPressed()
    {
           super.onBackPressed();
    }
    

    Declare this on your activity. super.OnBackPressed automatically calls back method in android. it will surely cancel your dialog.

    in addition, your dialog must look like this.

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder1.setMessage("TEST DIALOG.\n");
        builder1.setPositiveButton("Ok",
                new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    Toast.makeText(MainActivity.this, "This Is test Dialog", Toast.LENGTH_SHORT).show();
            }
        });
        AlertDialog alert11 = builder1.create();
        alert11.show();
    

    or you can set Negative button..

    Hope this helps!

    0 讨论(0)
  • 2021-01-03 03:49

    I understand that you are using the <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> permission for showing a floating view.

    Using a floating view you can intercept the back button press, but the home button press cannot be intercepted (android won't let you primarily because of security reasons).

    To intercept the back button press you need to add a wrapper when you inflate your floating view. Your wrapper should look like this:

        // Wrapper for intercepting System/Hardware key events
        ViewGroup wrapper = new FrameLayout(this) {
            @Override
            public boolean dispatchKeyEvent(KeyEvent event) {
                if (event.getKeyCode()==KeyEvent.KEYCODE_BACK) {
                    hideAddNotesFloatingView();
                    return true;
                }
                return super.dispatchKeyEvent(event);
            }
        };
    

    Then you add it to your floating view as a root:

            mAddNoteFloatingView = mInflater.inflate(R.layout.floating_add_note, wrapper);
    

    My complete code looks like this:

    private void addFloatingView() {
        final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                0,
                PixelFormat.TRANSLUCENT);
    
        params.gravity = Gravity.CENTER | Gravity.LEFT;
        params.x = 0;
        params.y = 0;
    
        // Wrapper for intercepting System/Hardware key events
        FrameLayout wrapper = new FrameLayout(this) {
            @Override
            public boolean dispatchKeyEvent(KeyEvent event) {
                if (event.getKeyCode()==KeyEvent.KEYCODE_BACK) {
                    // Add your code for handling the back button press
                    return true; // Return true means that the event was handled
                }
                return super.dispatchKeyEvent(event);
            }
        };
    
        mAddNoteFloatingView = mInflater.inflate(R.layout.floating_view, wrapper);
    
        mWindowManager.addView(mAddNoteFloatingView, params);
    }
    
    0 讨论(0)
  • 2021-01-03 03:53

    Define a custom layout and override dispatchKeyEvent, for example:

    public class CustomSystemAlertWindow extends FrameLayout {
    
        public static final String TAG = "CustomSystemAlertWindow";
    
        private WeakReference<Context> mContext;
    
        public CustomSystemAlertWindow(Context context) {
            super(context);
    
            mContext = new WeakReference<Context>(context);
    
            // Set a background color to identify the view on the screen
            this.setBackgroundColor(getResources().getColor(android.R.color.holo_red_light));
        }
    
        @Override
        public boolean dispatchKeyEvent(KeyEvent event) {
            if (event != null && event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
                Log.d(TAG, "back button pressed");
    
                if (mContext != null && mContext.get() != null) {
                    WindowManager wm = (WindowManager) mContext.get().getSystemService(Context.WINDOW_SERVICE);
                    wm.removeView(this);
                }
    
                return true;
            }
    
            return super.dispatchKeyEvent(event);
        }
    }
    

    Then add the view with this code:

    CustomSystemAlertWindow customSystemAlertWindow = new CustomSystemAlertWindow(context);
    
    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
            WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
            PixelFormat.TRANSLUCENT);
    
    WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
    wm.addView(customSystemAlertWindow, params);
    

    When you press the back button the view will dismiss.

    0 讨论(0)
  • 2021-01-03 03:57
    1. Show the Alert window through the Activity so you can detect it.

    Implement the code to detect easily Back Button or Home Button pressed.

    public class alertPopup extends Activity {
    
        Context context;
        final AlertDialog alertDialog;
        String TAG = "your Activity Name"
        boolean homePressed = false; // to detect the Homebutton pressed
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
         AlertDialog.Builder builder = newAlertDialog.Builder(YourActivity.this, R.style.AppCompatAlertDialogStyle);
         builder.setTitle("AlertDialog Title");
                ..........
                ....... // Build ur AlertDialog
    
         alertDialog= builder.create();
         alertDialog.show();
    
    
             //to detect Alert Dialog cancel when user touches outside the Dialog prompt
         alertDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {
                    Log.v(TAG,"Alert Dialog cancelled when user touches outside the Dialog prompt")
                }
            });
    
        }
    
    
        @Override
        public void onBackPressed()
        {
        Log.v(TAG,"Back Button Pressed");
         super.onBackPressed();
    
         alertDialog.dismiss();   //dismiss the alertDialog
         alertPopup.this.finish();  // Destroy the current activity
    
         homePressed = false;
        }
    
        @Override
        public void onResume() {
            super.onResume();
            homePressed = true; // default: other wise onBackPressed will set it to false
        }
    
    
        @Override
        public void onPause() {
            super.onPause();
            if(homePressed) { 
    
            alertDialog.dismiss();   //dismiss the alertDialog
            alertPopup.this.finish();  // Destroy the current activity
    
            Log.v(TAG, "Home Button Pressed"); }
        }
    
    
        public void onDestroy(){
    
            super.onDestroy();
        }
    
    
    }
    

    Note:

    Add this Permission in Android Manifest to show the alert Window .

     <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    

    Happy Coding :)

    0 讨论(0)
  • 2021-01-03 03:58

    Use the code below

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exitByBackKey();
    
            //moveTaskToBack(false);
    
            return true;
        }
        return super.onKeyDown(keyCode, event);
        }
    
        protected void exitByBackKey() {
    
        AlertDialog alertbox = new AlertDialog.Builder(this)
        .setMessage("Do you want to exit application?")
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    
            // do something when the button is clicked
            public void onClick(DialogInterface arg0, int arg1) {
    
                finish();
                //close();
    
    
            }
        })
        .setNegativeButton("No", new DialogInterface.OnClickListener() {
    
            // do something when the button is clicked
            public void onClick(DialogInterface arg0, int arg1) {
                           }
        })
          .show();
    
        }
    
    0 讨论(0)
  • 2021-01-03 03:58

    In addition to @Eliran Kuta's solution, this is more simple answer for Back button.

    val view = getAlertView()
    val windowParam = WindowManager.LayoutParams(
        WindowManager.LayoutParams.WRAP_CONTENT, // whatever
        WindowManager.LayoutParams.WRAP_CONTENT, // whatever
        WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, // use WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY before Oreo
        WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, // whatever
        PixelFormat.TRANSLUCENT // whatever
    )
    
    view.isFocusableInTouchMode = true
    view.setOnKeyListener { view, keyCode, event ->
        when (keyCode) {
            KeyEvent.KEYCODE_BACK -> {
                // do your work here
                true
            }
            else -> false
        }
    }
    
    val windowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManager
    windowManager.addView(view, windowParam)
    
    0 讨论(0)
提交回复
热议问题