如何在OS 7.0+版上关闭系统对话框或通知?

时间:2018-10-18 06:47:10

标签: android

我正在开发android应用,我想以编程方式关闭系统对话框。 我正在使用此代码关闭系统对话框,此代码在android 6.0及以下版本中正常运行,但问题是它在android 7.0和android 8.0中不起作用。如何解决这个问题呢。 请帮助我,谢谢。

Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
sendBroadcast(closeDialog);

1 个答案:

答案 0 :(得分:0)

如果您要关闭通知面板,

我正在为Android Oreo使用以下代码。

Handler collapseNotificationHandler;

@Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        Log.d(tag, "window focus changed");

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            collapseNow();
        }
    }


public void collapseNow() {

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

            // Post a Runnable with some delay - currently set to 300 ms
            collapseNotificationHandler.postDelayed(new Runnable() {

                @Override
                public void run() {

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

                    try {
                        statusBarManager = Class.forName("android.app.StatusBarManager");
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }

                    Method collapseStatusBar = null;
                    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) {
                        e.printStackTrace();
                    }

                    collapseStatusBar.setAccessible(true);

                    try {
                        collapseStatusBar.invoke(statusBarService);
                    } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    }
                    // Currently, the delay is 10 ms. You can change this
                    // value to suit your needs.
                    collapseNotificationHandler.postDelayed(this, 10L);
                }
            }, 10L);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }