Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓隐藏&;禁用通知(状态)栏_Android - Fatal编程技术网

Android 安卓隐藏&;禁用通知(状态)栏

Android 安卓隐藏&;禁用通知(状态)栏,android,Android,通过使用下面的代码,我可以全屏隐藏通知栏 android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen" 或 但是我要做的是完全禁用状态栏。我处于所谓的“信息亭模式”,我想确保用户不能将手指从顶部挡板上滑下。上述两种解决方案都可以隐藏通知栏,但无法在应用程序中完全禁用通知栏 这可能吗?以下是我所做的,而不是其他答案的链接 此解决方案不允许用户在下拉状态栏时“查看”其“预览”状态(即使在全屏应用程序中),但它不允许用户将状态栏下拉

通过使用下面的代码,我可以全屏隐藏通知栏

android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen"

但是我要做的是完全禁用状态栏。我处于所谓的“信息亭模式”,我想确保用户不能将手指从顶部挡板上滑下。上述两种解决方案都可以隐藏通知栏,但无法在应用程序中完全禁用通知栏


这可能吗?

以下是我所做的,而不是其他答案的链接

此解决方案不允许用户在下拉状态栏时“查看”其“预览”状态(即使在全屏应用程序中),但它不允许用户将状态栏下拉至其完整状态以查看设置、通知等

您必须首先在
AndroidManifest.xml

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/> 
设置好这两个选项后,您可以将其添加到主
onCreate()

此解决方案禁用始终向下拉状态栏的功能,直到您的应用程序关闭。如果不想每次都关闭应用程序,则必须在暂停时删除此操作


我认为永久禁用状态栏很困难。我也在研究同样的概念,做了大量的研发,发现下面的代码很有用。如果用户试图展开状态栏,那么在一秒钟内,它会将状态栏拉回,并在oreo上工作。我试过不同的操作系统

公共类BlockStatusBar{ 语境

    // To keep track of activity's window focus
    boolean currentFocus;
    // To keep track of activity's foreground/background status
    boolean isPaused;

    public Handler collapseNotificationHandler;
    Method collapseStatusBar = null;

    public BlockStatusBar(Context context, boolean isPaused) {
        this.context = context;
        this.isPaused = isPaused;
        collapseNow();
    }

    public void collapseNow() {


        // Initialize 'collapseNotificationHandler'
        if (collapseNotificationHandler == null) {
            collapseNotificationHandler = new Handler();

        }

        // If window focus has been lost && activity is not in a paused state
        // Its a valid check because showing of notification panel
        // steals the focus from current activity's window, but does not
        // 'pause' the activity
        if (!currentFocus && !isPaused) {

            Runnable myRunnable = new Runnable() {
                public void run() {
                    // do something
                    try {

                        // Use reflection to trigger a method from 'StatusBarManager'
                        Object statusBarService = context.getSystemService("statusbar");
                        Class<?> statusBarManager = null;

                        try {
                            statusBarManager = Class.forName("android.app.StatusBarManager");
                        } catch (ClassNotFoundException e) {
                            Log.e(LOG_TAG, "" + e.getMessage());
                        }

                        try {

                            // Prior to API 17, the method to call is 'collapse()'
                            // API 17 onwards, the method to call is `collapsePanels()`
                            if (Build.VERSION.SDK_INT > 16) {
                                collapseStatusBar = statusBarManager.getMethod("collapsePanels");
                            } else {
                                collapseStatusBar = statusBarManager.getMethod("collapse");
                            }
                        } catch (NoSuchMethodException e) {
                            Log.e(LOG_TAG, "" + e.getMessage());
                        }

                        collapseStatusBar.setAccessible(true);

                        try {
                            collapseStatusBar.invoke(statusBarService);
                        } catch (IllegalArgumentException e) {
                            e.printStackTrace();
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        } catch (InvocationTargetException e) {
                            e.printStackTrace();
                        }

                        // Check if the window focus has been returned
                        // If it hasn'kioskthread been returned, post this Runnable again
                        // Currently, the delay is 100 ms. You can change this
                        // value to suit your needs.
                        if (!currentFocus && !isPaused) {
                            collapseNotificationHandler.postDelayed(this, 100L);

                        }
                        if (!currentFocus && isPaused) {
                            collapseNotificationHandler.removeCallbacksAndMessages(null);
                        }
                    } catch (Exception e) {
                        Log.e("MSG", "" + e.getMessage());
                    }
                }
            };
            // Post a Runnable with some delay - currently set to 300 ms
            collapseNotificationHandler.postDelayed(myRunnable, 1L);

        }
    }
}
//跟踪活动的窗口焦点
布尔当前焦点;
//跟踪活动的前景/背景状态
布尔值是有原因的;
公共处理程序collapseNotificationHandler;
方法collapseStatusBar=null;
公共块状态栏(上下文上下文,布尔值isPaused){
this.context=上下文;
this.isPaused=isPaused;
collapseNow();
}
公共空间坍塌{
//初始化“collapseNotificationHandler”
if(collapseNotificationHandler==null){
collapseNotificationHandler=新处理程序();
}
//如果窗口焦点已丢失&&activity未处于暂停状态
//这是一个有效的检查,因为显示了通知面板
//从当前活动的窗口窃取焦点,但不会
//“暂停”活动
如果(!currentFocus&&!isPaused){
Runnable myRunnable=新Runnable(){
公开募捐{
//做点什么
试一试{
//使用反射从“StatusBarManager”触发方法
对象statusBarService=context.getSystemService(“statusbar”);
类statusBarManager=null;
试一试{
statusBarManager=Class.forName(“android.app.statusBarManager”);
}catch(classnotfounde异常){
Log.e(Log_标记,“+e.getMessage());
}
试一试{
//在API 17之前,要调用的方法是“collapse()
//从API 17开始,要调用的方法是`collapsePanels()`
如果(Build.VERSION.SDK_INT>16){
collapseStatusBar=statusBarManager.getMethod(“collapsePanels”);
}否则{
collapseStatusBar=statusBarManager.getMethod(“折叠”);
}
}捕获(无此方法例外){
Log.e(Log_标记,“+e.getMessage());
}
collapseStatusBar.setAccessible(true);
试一试{
调用(statusBarService);
}捕获(IllegalArgumentException e){
e、 printStackTrace();
}捕获(非法访问例外e){
e、 printStackTrace();
}捕获(调用TargetException e){
e、 printStackTrace();
}
//检查窗口焦点是否已返回
//如果尚未返回kioskthread,请再次发布此Runnable
//目前,延迟为100毫秒。您可以更改此设置
//满足您的需求的价值。
如果(!currentFocus&&!isPaused){
collapseNotificationHandler.postDelayed(此,100L);
}
如果(!currentFocus&&isPaused){
collapseNotificationHandler.removeCallbacksAndMessages(空);
}
}捕获(例外e){
Log.e(“MSG”和“+e.getMessage());
}
}
};
//发布具有一定延迟的Runnable-当前设置为300毫秒
collapseNotificationHandler.postDelayed(myRunnable,1L);
}
}
}

相同的问题,对吗?-@LinX64,答案与我这里的答案相同。用户希望以编程方式禁用通知栏,我想永久禁用它。请检查用于禁用通知栏的链接,以防止其被拉下。@FarazHussain,谢谢,根据该链接,我找到另一个链接,该链接有一个很好的答案:非常好的解决方案,但是如何删除此视图?因为我尝试使用
manager.removeView(视图)
manager.removevieimmediate(视图)
,而状态栏仍然被阻止,或者透明层位于顶部Thanks@SergioAntonioSnchezCamarero,我已经很久没有做这件事了
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;
    }
}
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);
    // To keep track of activity's window focus
    boolean currentFocus;
    // To keep track of activity's foreground/background status
    boolean isPaused;

    public Handler collapseNotificationHandler;
    Method collapseStatusBar = null;

    public BlockStatusBar(Context context, boolean isPaused) {
        this.context = context;
        this.isPaused = isPaused;
        collapseNow();
    }

    public void collapseNow() {


        // Initialize 'collapseNotificationHandler'
        if (collapseNotificationHandler == null) {
            collapseNotificationHandler = new Handler();

        }

        // If window focus has been lost && activity is not in a paused state
        // Its a valid check because showing of notification panel
        // steals the focus from current activity's window, but does not
        // 'pause' the activity
        if (!currentFocus && !isPaused) {

            Runnable myRunnable = new Runnable() {
                public void run() {
                    // do something
                    try {

                        // Use reflection to trigger a method from 'StatusBarManager'
                        Object statusBarService = context.getSystemService("statusbar");
                        Class<?> statusBarManager = null;

                        try {
                            statusBarManager = Class.forName("android.app.StatusBarManager");
                        } catch (ClassNotFoundException e) {
                            Log.e(LOG_TAG, "" + e.getMessage());
                        }

                        try {

                            // Prior to API 17, the method to call is 'collapse()'
                            // API 17 onwards, the method to call is `collapsePanels()`
                            if (Build.VERSION.SDK_INT > 16) {
                                collapseStatusBar = statusBarManager.getMethod("collapsePanels");
                            } else {
                                collapseStatusBar = statusBarManager.getMethod("collapse");
                            }
                        } catch (NoSuchMethodException e) {
                            Log.e(LOG_TAG, "" + e.getMessage());
                        }

                        collapseStatusBar.setAccessible(true);

                        try {
                            collapseStatusBar.invoke(statusBarService);
                        } catch (IllegalArgumentException e) {
                            e.printStackTrace();
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        } catch (InvocationTargetException e) {
                            e.printStackTrace();
                        }

                        // Check if the window focus has been returned
                        // If it hasn'kioskthread been returned, post this Runnable again
                        // Currently, the delay is 100 ms. You can change this
                        // value to suit your needs.
                        if (!currentFocus && !isPaused) {
                            collapseNotificationHandler.postDelayed(this, 100L);

                        }
                        if (!currentFocus && isPaused) {
                            collapseNotificationHandler.removeCallbacksAndMessages(null);
                        }
                    } catch (Exception e) {
                        Log.e("MSG", "" + e.getMessage());
                    }
                }
            };
            // Post a Runnable with some delay - currently set to 300 ms
            collapseNotificationHandler.postDelayed(myRunnable, 1L);

        }
    }
}