Android 开发技巧之 禁止截屏和屏幕常亮

 

1 禁止截屏

        出于安全考虑,我们需要对某些界面,比如二维码界面,登录界面禁止截屏。(像支付宝的付款二维码

  禁止截屏的代码很简单就一行,在Activity 生命初期的onCreate加上:

 getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
   这样你截屏的时候,系统会通知你截屏失败(华为是通知栏的方式),FLAG_SECURE 源码解释:

/** Window flag: treat the content of the window as secure, preventing
         * it from appearing in screenshots or from being viewed on non-secure
         * displays.
         *
         * 

See {@link android.view.Display#FLAG_SECURE} for more details about * secure surfaces and secure displays. */ public static final int FLAG_SECURE = 0x00002000;


2 屏幕常亮

 
 当我们用真机调试程序的时候,往往被锁屏烦恼,有一种办法比手动调节固定时间锁屏好用,在相应Activity 的初始周期onCreate 上加上:

 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
这样,当前界面就能保持常亮。FLAG_KEEP_SCREEN_ON 源码解释:

/** Window flag: as long as this window is visible to the user, keep
         *  the device's screen turned on and bright. */
        public static final int FLAG_KEEP_SCREEN_ON     = 0x00000080;



你可能感兴趣的:(Activity)