简体   繁体   中英

Android Close/Hide Notification Bar

I got a problem when I tried to close the notification bar from my application.

here is where i try to close it

@Override
    public void onWindowFocusChanged(boolean hasFocus)
    {
        Log.i("FocusChanged", "Focus changed");
        if(!hasFocus)
        {
            Log.e("Focus debug", "Lost focus !");
// Close every kind of system dialog
            Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
            getApplicationContext().sendBroadcast(closeDialog);
            Log.i("FocusChanged", "Focus changed - iam supposed to close this dialogs");
        }
        super.onWindowFocusChanged(hasFocus);
    }

But it don't work. Unless if I add a breakpoint to the function.

Everytime that i open the system notification i got the log that say "Focus changed" so my app run the code, but hasFocus not set to true, or as I said only if i ran the debugger and set breakpoint in the function.

We could not prevent the status appearing in full screen mode in kitkat devices, so made a hack which still suits the requirement ie block the status bar from expanding.

For that to work, the app was not made full screen. We put a overlay over status bar and consumed all input events. It prevented the status from expanding.

note:

  • customViewGroup is custom class which extends any layout(frame,relative layout etc) and consumes touch event.
  • to consume touch event override the onInterceptTouchEvent method of the view group and return true

You also need to set the android permission

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

You need an extended ViewGroup class to handle Touch Event

public class customViewGroup extends ViewGroup {

        public customViewGroup(Context context) {
            super(context);
        }

        @Override
        protected void onLayout(boolean changed, int l, int t, int r, int b) {
        }

        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            Log.v("customViewGroup", "**********Intercepted");
            return true;
        }
    }

in the activity where you want to disable the notification bar you need to add this function (don't forget to add field variable CustomViewGroup blockingView)

private void disableNotificationBar(){
    WindowManager manager = ((WindowManager) getApplicationContext()
                .getSystemService(Context.WINDOW_SERVICE));

    WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams();
    localLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
    localLayoutParams.gravity = Gravity.TOP;
    localLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|

                // this is to enable the notification to recieve touch events
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |

                // Draws over status bar
                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;

        localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        localLayoutParams.height = (int) (50 * getResources()
                .getDisplayMetrics().scaledDensity);
        localLayoutParams.format = PixelFormat.TRANSPARENT;

        blockingView = new CustomViewGroup(this);

        manager.addView(blockingView, localLayoutParams);
}

and then in you onCreate you need to call

protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        ....
        disableNotificationBar();
}

You need to remove it on the onDestroy() to be able to close your application

@Override
protected void onDestroy() {

    super.onDestroy();
    if (blockingView!=null) {
        WindowManager manager = ((WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE));
        manager.removeView(blockingView);
    }
}

You also need to adjust the overlay size, according to previous answer 40 is enought, but you don't want to prevent user from doing action on your own app, if overlay is to height it will intercept event on your Activity too.

Also in android version > to 5.1 you need an extra permission from the user, add this on your onCreate function insted of the function call

if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

            if (!Settings.canDrawOverlays(this)) {
                Toast.makeText(this, "Please give my app this permission!", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,Uri.parse("package:" + getPackageName()));
                startActivityForResult(intent, OVERLAY_PERMISSION_REQ_CODE);
            } else {
                disableNotificationBar();
            }
        }
        else {
            disableNotificationBar();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM