昨天项目有个需求,要在app启动页上动态申请权限。要求启动页全覆盖手机屏幕。

首先启动页Activity要全屏显示。

1. 隐藏ActionBar:

ActionBar actionBar = getActionBar();if (actionBar != null) {    actionBar.hide();}

如果是继承AppCompatActivity,就用getSupportActionBar()。

2. 隐藏状态栏

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);

通过这两个步就可以全屏显示启动页了。

然而,当开始动态申请权限,弹出系统的权限提示对话框后,状态栏又重新露出来了。我日,不是隐藏了吗?怎么又出来了,什么鬼?

通过查看源码的解释:

/** * Request that the visibility of the status bar or other screen/window * decorations be changed. * * 

This method is used to put the over device UI into temporary modes * where the user's attention is focused more on the application content, * by dimming or hiding surrounding system affordances. This is typically * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content * to be placed behind the action bar (and with these flags other system * affordances) so that smooth transitions between hiding and showing them * can be done. * *

Two representative examples of the use of system UI visibility is * implementing a content browsing application (like a magazine reader) * and a video playing application. * *

The first code shows a typical implementation of a View in a content * browsing application. In this implementation, the application goes * into a content-oriented mode by hiding the status bar and action bar, * and putting the navigation elements into lights out mode. The user can * then interact with content while in this mode. Such an application should * provide an easy way for the user to toggle out of the mode (such as to * check information in the status bar or access notifications). In the * implementation here, this is done simply by tapping on the content. * * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java * content} * *

This second code sample shows a typical implementation of a View * in a video playing application. In this situation, while the video is * playing the application would like to go into a complete full-screen mode, * to use as much of the display as possible for the video. When in this state * the user can not interact with the application; the system intercepts * touching on the screen to pop the UI out of full screen mode. See * {@link #fitSystemWindows(Rect)} for a sample layout that goes with this code. * * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java * content} * * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE}, * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN}, * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION}, * {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}, {@link #SYSTEM_UI_FLAG_IMMERSIVE}, * and {@link #SYSTEM_UI_FLAG_IMMERSIVE_STICKY}. */

从释义上可以知道,setSystemUiVisibility()是用于使系统UI进入一种临时的模式,目的是使用户的注意力关注于应用程序的内容上。所以单独一个Activity这样设置是可以全屏显示的,这个只对当前的Activity有效。可是当申请系统权限使,弹出的对话框是系统的Activity,通过adb shell dumpsys activity 来看,当前最顶端的Activity已经是GrantPermissionsActivity。

Run #2: ActivityRecord{2b99111 u0 com.google.android.packageinstaller/com.android.packageinstaller.permission.ui.GrantPermissionsActivity t141}

而这个GrantPermissionsActivity,我们并无法去设置它的setSystemUiVisibility()。所以这种方法不奏效。

通过和同事讨论,后来找到一种方法,可以实现我们的需求。

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

这种方法是OK的。

它的源码释义是:

/** * Set the flags of the window, as per the * {@link WindowManager.LayoutParams WindowManager.LayoutParams} * flags. * * 

Note that some flags must be set before the window decoration is * created (by the first call to * {@link #setContentView(View, android.view.ViewGroup.LayoutParams)} or * {@link #getDecorView()}: * {@link WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN} and * {@link WindowManager.LayoutParams#FLAG_LAYOUT_INSET_DECOR}. These * will be set for you based on the {@link android.R.attr#windowIsFloating} * attribute. * * @param flags The new window flags (see WindowManager.LayoutParams). * @param mask Which of the window flag bits to modify. * @see #addFlags * @see #clearFlags */public void setFlags(int flags, int mask) {}

仔细分析发现,这个是设置整个当前Window的,而setSystemUiVisibility()聚焦于显示Activity内容的,还是有差别的。

/** * Window flag: hide all screen decorations (such as the status bar) while * this window is displayed.  This allows the window to use the entire * display space for itself -- the status bar will be hidden when * an app window with this flag set is on the top layer. A fullscreen window * will ignore a value of {@link #SOFT_INPUT_ADJUST_RESIZE} for the window's * {@link #softInputMode} field; the window will stay fullscreen * and will not resize. * * 

This flag can be controlled in your theme through the * {@link android.R.attr#windowFullscreen} attribute; this attribute * is automatically set for you in the standard fullscreen themes * such as {@link android.R.style#Theme_NoTitleBar_Fullscreen}, * {@link android.R.style#Theme_Black_NoTitleBar_Fullscreen}, * {@link android.R.style#Theme_Light_NoTitleBar_Fullscreen}, * {@link android.R.style#Theme_Holo_NoActionBar_Fullscreen}, * {@link android.R.style#Theme_Holo_Light_NoActionBar_Fullscreen}, * {@link android.R.style#Theme_DeviceDefault_NoActionBar_Fullscreen}, and * {@link android.R.style#Theme_DeviceDefault_Light_NoActionBar_Fullscreen}.

*/public static final int FLAG_FULLSCREEN = 0x00000400;

从释义上得知,设置这个flag可以隐藏所有的屏幕修饰,像status bar,用于Window使用整个显示屏。这个完全是我们的目的了。

 

更多相关文章

  1. Nginx系列教程(六)| 手把手教你搭建 LNMP 架构并部署天空网络电影
  2. Android(安卓)camera系统开发之IPC (三)
  3. Android(安卓)ethernet support
  4. android poweroff 流程
  5. Android(安卓)SELinux
  6. 【Android】 字体适配——不跟随系统字体大小、动态设置字体大小
  7. android for opencv (2) byte[] ,Bitmap 与 Mat 类型转换,Bitmap保
  8. android 8.1 Not allowed to start service Intent 无法后台开启
  9. android studio 权限类,不断更新中 如果你有权限我这边没有 请告

随机推荐

  1. 自学Android,学多久可以加入字节跳动?
  2. Android(安卓)ApiDemos示例解析(103):Vie
  3. Android(安卓)从原型模式看java实例化对
  4. android debug 研究
  5. Android(安卓)Studio 项目混淆打包时,报错
  6. android tips(android零碎心得)
  7. Support Annotation Library使用详解
  8. Android开发环境——SDK相关内容汇总
  9. Android模拟器入门
  10. Android(安卓)分辨率 及 px, dip相互转换