与状态栏交互有两种情况:
- 案例1:如果你的activity已经隐藏了状态栏,并且用户在没有显示通知的情况下拉下状态栏区域:这可以通过注册
OnSystemUiVisibilityChangeListener
监听器来获得系统UI可见性变化的通知来处理
boolean mStatusBarShown;
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.
mStatusBarShown = true;
} 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.
mStatusBarShown = false;
}
}
});
- 情况2:如果状态栏已经显示给用户,用户拉下显示通知区域;要检测到这一点,您需要
onWindowFocusChanged(boolean hasFocus)
在活动中进行覆盖,其中hasFocus
参数值将是“假”,以防用户拉下状态栏,以及当用户再次向上推状态栏时;然后onWindowFocusChanged
将再次调用,但这次hasFocus
将是true
@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);
}
检查此链接以获取更多信息。