开发者

How to open or expand status bar through intent?

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 w开发者_如何学编程ay 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!


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";


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;
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜