How to open or expand status bar through intent?

2019-01-25 21:32发布

问题:

I am making a home application and I think that it will be suitable if I use a fullscreen and not show the status bar. So now I want to be able to open or expand the status bar with a button on the menu, similar to the way some default home applications have in the menu. I know its possible since the default home does it. Is this done through an intent? If so can I have the code for it. If not well then I would appreciate it if you guys showed me how. Thanks!

回答1:

See if this helps and let me know...

 try{

   Object service  = getSystemService("statusbar");
  Class<?> statusbarManager = Class.forName("android.app.StatusBarManager");
  Method expand = statusbarManager.getMethod("expand");
  expand.invoke(service);

}
catch(Exception ex){
 ....
}

uses permission : "android.permission.EXPAND_STATUS_BAR";


回答2:

The code below works for me:

boolean shown = true; 
private void showHide() {
    Window w = this.getWindow();
if(shown)
{
    w.setFlags(0, 
             WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
else
{
    w.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
             WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
shown=!shown;
}


回答3:

This one worked for me:

manifest:

<uses-permission android:name="android.permission.EXPAND_STATUS_BAR"/>

Code:

//    https://gist.github.com/XinyueZ/7bad2c02be425b350b7f requires permission: "android.permission.EXPAND_STATUS_BAR"
@SuppressLint("WrongConstant", "PrivateApi")
@JvmStatic
fun setExpandNotificationDrawer(context: Context, expand: Boolean) {
    try {
        val statusBarService = context.getSystemService("statusbar")
        val methodName =
                if (expand)
                    if (Build.VERSION.SDK_INT >= 17) "expandNotificationsPanel" else "expand"
                else
                    if (Build.VERSION.SDK_INT >= 17) "collapsePanels" else "collapse"
        val statusBarManager: Class<*> = Class.forName("android.app.StatusBarManager")
        val method: Method = statusBarManager.getMethod(methodName)
        method.invoke(statusBarService)
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

However, someone wrote it doesn't work on all devices and Android versions (here), so I wrote a request to add official API for this, here.