【发布时间】:2012-01-02 07:18:02
【问题描述】:
我有一项服务应该定期检查状态栏的可见性,当某些顶级活动处于(或不)处于全屏模式时。 有可能吗?
【问题讨论】:
标签: android android-layout statusbar
我有一项服务应该定期检查状态栏的可见性,当某些顶级活动处于(或不)处于全屏模式时。 有可能吗?
【问题讨论】:
标签: android android-layout statusbar
最后我发现了如何检查状态栏是否可见。它是某种黑客,但它对我有用。我在我的服务中创建了该方法:
private void createHelperWnd() {
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
final WindowManager.LayoutParams p = new WindowManager.LayoutParams();
p.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
p.gravity = Gravity.RIGHT | Gravity.TOP;
p.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
p.width = 1;
p.height = LayoutParams.MATCH_PARENT;
p.format = PixelFormat.TRANSPARENT;
helperWnd = new View(this); //View helperWnd;
wm.addView(helperWnd, p);
final ViewTreeObserver vto = helperWnd.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (heightS == helperWnd.getHeight()) {
isFullScreen = true;
} else {
isFullScreen = false;
}
}
});
}
其中 widthS 和 heightS 是我们的全局屏幕尺寸; 在这里,我只是将不可见的帮助窗口高度与屏幕高度进行了比较,并决定状态栏是否可见。并且不要忘记在您的 Service 的 onDestroy 中删除 helperWnd。
【讨论】:
你好,如果你试试这个提供 android 作为良好实践的代码
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.
}
}
});
我留下文档的链接:https://developer.android.com/training/system-ui/visibility#java
【讨论】:
public boolean isStatusBarVisible() {
Rect rectangle = new Rect();
Window window = getActivity().getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
int statusBarHeight = rectangle.top;
return statusBarHeight != 0;
}
【讨论】:
不是真的。如果它是 您的 活动在前台,您的活动可以告诉服务它是否正在使用隐藏状态栏的主题。但是,您无法独立于服务来确定这一点,更不用说第三方活动而不是您自己的活动了。
【讨论】: