禁止截屏方法

由于项目涉及到公司的一些重要文件,所以需要禁止截屏,防止信息外漏。
截图工具类

ScreenUtils
public class ScreenUtils {
private ScreenUtils() {
/* cannot be instantiated */
throw new UnsupportedOperationException("cannot be instantiated");
}
/**
* 获得屏幕高度
*
*@paramcontext
*@return
*/
public static int getScreenWidth(Contextcontext) {
WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics =newDisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
return outMetrics.widthPixels;
}
/**
* 获得屏幕宽度
*
*@paramcontext
*@return
*/
public static int getScreenHeight(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics =newDisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
returnoutMetrics.heightPixels;
}
/**
* 获得状态栏的高度
*
*@paramcontext
*@return
*/
public static int getStatusHeight(Context context) {
intstatusHeight = -1;
try{
Class clazz = Class.forName("com.android.internal.R$dimen");
Object object = clazz.newInstance();
intheight = Integer.parseInt(clazz.getField("status_bar_height")
.get(object).toString());
statusHeight = context.getResources().getDimensionPixelSize(height);
}catch(Exception e) {
e.printStackTrace();
}
return statusHeight;
}
/**
* 获取当前屏幕截图,包含状态栏
*
*@paramactivity
*@return
*/
public static Bitmap snapShotWithStatusBar(Activity activity) {
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bmp = view.getDrawingCache();
intwidth =getScreenWidth(activity);
intheight =getScreenHeight(activity);
Bitmap bp =null;
bp = Bitmap.createBitmap(bmp,0,0,width,height);
view.destroyDrawingCache();
return bp;
}
/**
* 获取当前屏幕截图,不包含状态栏
*
*@paramactivity
*@return
*/
public static Bitmap snapShotWithoutStatusBar(Activity activity) {
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bmp = view.getDrawingCache();
Rect frame =newRect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
int width =getScreenWidth(activity);
int height =getScreenHeight(activity);
Bitmap bp =null;
bp = Bitmap.createBitmap(bmp,0,statusBarHeight,width,height
- statusBarHeight);
view.destroyDrawingCache();
return bp;
}

禁止截图的实现
禁止截图通过对window对象加标志位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;
使用方式如下:
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
  }

转自作者:一点点政府。(感谢前辈)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,122评论 25 708
  • 截屏,或者说截图,大概是我们除了复制/粘贴以外,比较常用的一个操作了.一般来说,有屏幕的地方就可以截屏.譬如电脑的...
    半坡城主阅读 4,357评论 0 17
  • 你会喝水吗? 白开水:排毒瘦小腹,一般人最好的选择,早晨来一杯; 盐水:加快肠胃蠕动排出毒素,便秘者适用,适合清晨...
    梅子绿茶_30a8阅读 87评论 0 0
  • 湖南投资理财学会众大咖借节假日休息之日,先参观了微谷科技,受到微谷科技CEO西栅大叔热情接待,晚上则相聚岳麓山上合...
    村子老师阅读 332评论 0 2
  • 《庄子》解,每章一读。 文: 圣人达绸缪1,周尽一体矣,而不知其然,性也。复命摇作而以天为师,人则从而命之也。忧乎...
    千里飘蓬阅读 702评论 0 0