Android Hide&禁用通知(状态)栏 [英] Android Hide & Disable Notification (Status) Bar
问题描述
通过使用以下代码,我可以全屏显示来隐藏通知栏
I've been able to hide the notification bar by going full screen, by using the code below
android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen"
或
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
但是我想做的是完全禁用状态栏.我处于所谓的信息亭模式",我想确保用户不能从顶部挡板向下滑动手指.以上两种解决方案都可以隐藏通知栏,但不能在应用程序中完全禁用它.
But what I am trying to do is completely disable the status bar. I'm in what's known as a "kiosk mode" and I'd like to make sure that a user can not slide their finger down from the top bezel. Both of the solutions above work for hiding the notification bar, but it does not work for disabling it completely within the app.
这可能吗?
推荐答案
这是我所做的,而不是跟随其他答案的链接.
Instead of following links to other answers, here's what I did.
此解决方案不允许用户将其下拉(即使在全屏应用中),也不能以预览"状态查看"状态栏,但是却不允许用户将状态栏下拉到完整状态状态以查看设置,通知等.
This solution does not disallow a user to "view" the status bar in it's 'preview' state if pulled down (even in a full screen app), but it DOES disallow a user from pulling the status bar down to it's full state to see settings, notifications, etc.
您必须首先在 AndroidManifest.xml
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
然后添加另一个名为 customViewGroup.java
的类(Java文件),并将此代码放入其中:
Then add another class (Java file) called customViewGroup.java
and place this code in it:
import android.content.Context;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewGroup;
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;
}
}
完成这两项设置后,您可以将其添加到主 onCreate()
After you have both of those set up, you can then add this into your main onCreate()
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;
customViewGroup view = new customViewGroup(this);
manager.addView(view, localLayoutParams);
此解决方案将始终禁用下拉状态栏的功能,直到您的应用关闭.如果您不想每次都关闭应用程序,则必须暂停执行此操作.
This solution disables the ability to pull the status bar down always, until your app is closed. You'll have to remove this action on pause if you don't want to close your app every time.
贷方来自
Credit goes to @Abhimaan Madhav from This Answer
这篇关于Android Hide&禁用通知(状态)栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!