Android: listener on the status bar notification

爱⌒轻易说出口 提交于 2020-01-04 07:55:06

问题


android:Is there a way to listen to the status bar notification when it is being pulled down?


回答1:


1. For detecting status bar changes: you can register a listener to get notified of system UI visibility changes

put below code into your activity

     // Detecting if the user swipe from the top down to the phone screen to show up the status bar
    // (without showing the notification area); so we could set mStatusBarShow flat to true to allow
    // us hide the status bar for the next onClick event

    View decorView = getWindow().getDecorView();
    decorView.setOnSystemUiVisibilityChangeListener
            (new View.OnSystemUiVisibilityChangeListener() {
                @Override
                public void onSystemUiVisibilityChange(int visibility) {
                    // Note that system bars will only be "visible" if none of the
                    // LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.
                    if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                        // TODO: The system bars are visible. Make any desired
                        // adjustments to your UI, such as showing the action bar or
                        // other navigational controls.

                    } else {
                        // TODO: The system bars are NOT visible. Make any desired
                        // adjustments to your UI, such as hiding the action bar or
                        // other navigational controls.

                    }
                }
            });

check out this link for more detail

2. For detecting if the user pulls down the notification area of the status bar: override onWindowFocusChanged() in your activity

@Override
public void onWindowFocusChanged(boolean hasFocus) {

    // handle when the user pull down the notification bar where
    // (hasFocus will ='false') & if the user pushed the
    // notification bar back to the top, then (hasFocus will ='true')
    if (!hasFocus) {
        Log.i("Tag", "Notification bar is pulled down");
    } else {
        Log.i("Tag", "Notification bar is pushed up");
    }
    super.onWindowFocusChanged(hasFocus);

}

check out the solution of Stephan Palmer in this link




回答2:


Take a look on Notification.Builder setDeleteIntent (since api level 11).

There is also Notification deleteIntent (since api level 1)



来源:https://stackoverflow.com/questions/11520465/android-listener-on-the-status-bar-notification

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