android开发之拦截用户截屏,并删除截屏文件

2017-06-10  本文已影响247人  从心开始的我

特殊的需求造就特殊的功能

有的付费页面是不能让用户截屏分享的,所以...我们就流氓的拦截掉并且删除

实现方式

代码如下

/**
 * Created by wangfei on 2016/8/1 19:02.
 * 拦截用户截屏,并且删除截屏文件
 */
public class ScreenshotContentObserver {

    private Context mContext;
    private HandlerThread handlerThread;
    private Handler handler;
    private static ScreenshotContentObserver instance;
    private static final String TAG = "WWW";
    private static final String EXTERNAL_CONTENT_URI_MATCHER =
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString();
    private static final String[] PROJECTION = new String[]{
            MediaStore.Images.Media.DISPLAY_NAME, MediaStore.Images.Media.DATA,
            MediaStore.Images.Media.DATE_ADDED
    };
    private static final String SORT_ORDER = MediaStore.Images.Media.DATE_ADDED + " DESC";
    private ContentResolver mContentResolver;
    private ContentObserver mContentObserver;

    private ScreenshotContentObserver(Context context) {
        mContext = context;
        handlerThread = new HandlerThread("content_observer");
        handlerThread.start();
        handler = new Handler(handlerThread.getLooper()) {

            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                //删除截屏
                ((File)msg.obj).delete();
                // 最后通知图库更新
                mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile((File)msg.obj)));
            }
        };
    }

    public static void startObserve(Context context) {
        if (instance == null) {
            instance = new ScreenshotContentObserver(context);
        }
        instance.register();
    }

    public static void stopObserve() {
        instance.unregister();
    }

    private void register() {
        mContentResolver = mContext.getContentResolver();
        mContentObserver = new ContentObserver(handler) {
            @Override
            public void onChange(boolean selfChange, Uri uri) {
                Log.d(TAG, "onChange: " + selfChange + ", " + uri.toString());
                if (uri.toString().matches(EXTERNAL_CONTENT_URI_MATCHER)) {
                    Cursor cursor = null;
                    try {
                        cursor = mContentResolver.query(uri, PROJECTION, null, null, SORT_ORDER);
                        if (cursor != null && cursor.moveToFirst()) {
                            String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
                            long dateAdded = cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media.DATE_ADDED));
                            long currentTime = System.currentTimeMillis();
                            Log.d(TAG, "path: " + path + ", dateAdded: " + dateAdded + ", currentTime: " + currentTime);
                            Log.d(TAG, "matchPath(path): " + matchPath(path) + ", matchAddTime(dateAdded): " +
                                    matchAddTime(dateAdded) + ", matchSize(path): " + matchSize(path));
                            if (matchPath(path) && matchAddTime(dateAdded)) {
                                // screenshot added!
                                File file = new File(path);
                                Message message=Message.obtain();
                                message.obj=file;
                                //解决华为手机无法删除截屏问题
                                handler.sendMessageDelayed(message,250);
                            }
                        }
                    } catch (Exception e) {
                        Log.d(TAG, "Exception" + e.toString());
                    } finally {
                        if (cursor != null) {
                            cursor.close();
                        }
                    }
                }
                super.onChange(selfChange, uri);
            }
        };
        mContentResolver.registerContentObserver(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, true,
                mContentObserver);
    }

    private void unregister() {
        mContext.getContentResolver().unregisterContentObserver(mContentObserver);
    }


    /**
     * 添加时间与当前时间不超过1.5s,大部分时候不超过1s。
     *
     * @param addTime 图片添加时间,单位:秒
     */
    private boolean matchAddTime(long addTime) {
        return System.currentTimeMillis() - addTime * 1000 < 1500;
    }

    /**
     * 尺寸不大于屏幕尺寸
     */
    private boolean matchSize(String filePath) {
        int w = ScreenUtil.getScreenWidth(mContext);
        int h = ScreenUtil.getScreenWidth(mContext);

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);

        return w >= options.outWidth && h >= options.outHeight;
    }

    /**
     * 已调查的手机截屏图片的路径中带有screenshot
     */
    private boolean matchPath(String filePath) {
        String lower = filePath.toLowerCase();
        return lower.contains("screenshot");
    }

}

使用方式(Activity中)


    protected void onStart() {
        super.onStart();
        ScreenshotContentObserver.startObserve(this);
    }



    protected void onStop() {
        super.onStop();
        ScreenshotContentObserver.stopObserve();
    }

最重要的一步

加权限

android.permission.READ_EXTERNAL_STORAGE

搞定..........

上一篇下一篇

猜你喜欢

热点阅读