在Android App中监听体系截屏功用,没有体系标准的监听器或者api能够调用,需求自己完成。

针对这个需求,现在大部分完成方案是监听体系的媒体数据库

原理:每逢产生一张新图片,体系都会把这张图片的详细信息加入到媒体数据库,并宣布内容改动告诉。

完成:利用内容调查者(ContentObserver)监听媒体数据库的改变,当数据库有改变时,获取最终刺进的一条图片数据,假如该图片契合特定的规矩,则以为用户截屏了。

监听两个Uri

  • 内部存储空间的 content:// 格局Uri:MediaStore.Images.Media.INTERNAL_CONTENT_URI
  • 首要外部存储空间 content:// 格局Uri:MediaStore.Images.Media.EXTERNAL_CONTENT_URI

权限:开始监听媒体数据库改变之前,需求先获取权限READ_EXTERNAL_STORAGE

完成过程

1、定义一个内容调查者类:

/**
 * 媒体内容调查者(调查媒体数据库的改动)
 */
private class MediaContentObserver extends ContentObserver {
    private Uri mContentUri;
    public MediaContentObserver(Uri contentUri, Handler handler) {
        super(handler);
        mContentUri = contentUri;
    }
    @Override
    public void onChange(boolean selfChange) {
        LogUtils.e("ScreenShotListenManager  MediaContentObserver  onChange");
        super.onChange(selfChange);
        //经过媒体数据库的内容改动来判别用户是否履行了截屏操作
    }
}

2、创立并注册内容调查者

// 创立内容调查者
mInternalObserver = new MediaContentObserver(MediaStore.Images.Media.INTERNAL_CONTENT_URI,mUiHandler);
mExternalObserver = new MediaContentObserver(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,mUiHandler);
// 注册内容调查者
mContext.getContentResolver().registerContentObserver(
        MediaStore.Images.Media.INTERNAL_CONTENT_URI,
        Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q,  //Android Q适配
        mInternalObserver
);
mContext.getContentResolver().registerContentObserver(
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
        Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q,  //Android Q适配
        mExternalObserver
);

Build.VERSION_CODES.Q(29),编译的sdk需求>=29

3、不需求监听内容改变时,一定要刊出内容调查者

// 刊出内容调查者
if (mInternalObserver != null) {
    try {
        mContext.getContentResolver().unregisterContentObserver(mInternalObserver);
    } catch (Exception e) {
        e.printStackTrace();
    }
    mInternalObserver = null;
}
if (mExternalObserver != null) {
    try {
        mContext.getContentResolver().unregisterContentObserver(mExternalObserver);
    } catch (Exception e) {
        e.printStackTrace();
    }
    mExternalObserver = null;
}

当媒体内容发生改变时,会履行ContentObserver 的onChange方法。咱们需求经过媒体数据库的内容改动来判别用户是否履行了截屏操作: 获取最近刺进的一条数据,判别是否契合截屏图片的特征。假如契合,那么咱们以为用户进行了截图操作。

public class ScreenShotListenManager {
    private static final String TAG = "ScreenShotListenManager";
    /**
     * 读取媒体数据库时需求读取的列
     */
    private static final String[] MEDIA_PROJECTIONS = {
            MediaStore.Images.ImageColumns.DATA,
            MediaStore.Images.ImageColumns.DATE_TAKEN,
    };
    /**
     * 读取媒体数据库时需求读取的列, 其中 WIDTH 和 HEIGHT 字段在 API 16 以后才有
     */
    private static final String[] MEDIA_PROJECTIONS_API_16 = {
            MediaStore.Images.ImageColumns.DATA,
            MediaStore.Images.ImageColumns.DATE_TAKEN,
            MediaStore.Images.ImageColumns.WIDTH,
            MediaStore.Images.ImageColumns.HEIGHT,
    };
    /**
     * 截屏根据中的途径判别关键字
     */
    private static final String[] KEYWORDS = {
            "screenshot", "screen_shot", "screen-shot", "screen shot",
            "screencapture", "screen_capture", "screen-capture", "screen capture",
            "screencap", "screen_cap", "screen-cap", "screen cap"
    };
    private static Point sScreenRealSize;
    /**
     * 已回调过的途径
     */
    private final static List<String> sHasCallbackPaths = new ArrayList<String>();
    private Context mContext;
    private OnScreenShotListener mListener;
    private long mStartListenTime;
    /**
     * 内部存储器内容调查者
     */
    private MediaContentObserver mInternalObserver;
    /**
     * 外部存储器内容调查者
     */
    private MediaContentObserver mExternalObserver;
    /**
     * 运行在 UI 线程的 Handler, 用于运行监听器回调
     */
    private final Handler mUiHandler = new Handler(Looper.getMainLooper());
    private ScreenShotListenManager(Context context) {
        if (context == null) {
            throw new IllegalArgumentException("The context must not be null.");
        }
        mContext = context;
        // 获取屏幕实在的分辨率
        if (sScreenRealSize == null) {
            sScreenRealSize = getRealScreenSize();
            if (sScreenRealSize != null) {
                Log.d(TAG, "Screen Real Size: " + sScreenRealSize.x + " * " + sScreenRealSize.y);
            } else {
                Log.e(TAG, "Get screen real size failed.");
            }
        }
    }
    public static ScreenShotListenManager newInstance(Context context) {
        assertInMainThread();
        return new ScreenShotListenManager(context);
    }
    /**
     * 启动监听
     */
    public void startListen() {
        assertInMainThread();
        // 记录开始监听的时刻戳
        mStartListenTime = System.currentTimeMillis();
        // 创立内容调查者
        mInternalObserver = new MediaContentObserver(MediaStore.Images.Media.INTERNAL_CONTENT_URI, mUiHandler);
        mExternalObserver = new MediaContentObserver(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, mUiHandler);
        // 注册内容调查者
        mContext.getContentResolver().registerContentObserver(
                MediaStore.Images.Media.INTERNAL_CONTENT_URI,
                Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q,
                mInternalObserver
        );
        mContext.getContentResolver().registerContentObserver(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q,
                mExternalObserver
        );
        LogUtils.e("ScreenShotListenManager  startListen");
    }
    /**
     * 中止监听
     */
    public void stopListen() {
        assertInMainThread();
        // 刊出内容调查者
        if (mInternalObserver != null) {
            try {
                mContext.getContentResolver().unregisterContentObserver(mInternalObserver);
            } catch (Exception e) {
                e.printStackTrace();
            }
            mInternalObserver = null;
        }
        if (mExternalObserver != null) {
            try {
                mContext.getContentResolver().unregisterContentObserver(mExternalObserver);
            } catch (Exception e) {
                e.printStackTrace();
            }
            mExternalObserver = null;
        }
        // 清空数据
        mStartListenTime = 0;
//        sHasCallbackPaths.clear();
        //切记!!!:必须设置为空 可能mListener 会隐式持有Activity导致释放不掉
        mListener = null;
    }
    /**
     * 处理媒体数据库的内容改动
     */
    private void handleMediaContentChange(Uri contentUri) {
        Cursor cursor = null;
        try {
            // 数据改动时查询数据库中最终加入的一条数据
            cursor = mContext.getContentResolver().query(
                    contentUri,
                    Build.VERSION.SDK_INT < 16 ? MEDIA_PROJECTIONS : MEDIA_PROJECTIONS_API_16,
                    null,
                    null,
                    MediaStore.Images.ImageColumns.DATE_ADDED + " desc limit 1"
            );
            if (cursor == null) {
                Log.e(TAG, "Deviant logic.");
                return;
            }
            if (!cursor.moveToFirst()) {
                Log.d(TAG, "Cursor no data.");
                return;
            }
            // 获取各列的索引
            int dataIndex = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
            int dateTakenIndex = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATE_TAKEN);
            int widthIndex = -1;
            int heightIndex = -1;
            if (Build.VERSION.SDK_INT >= 16) {
                widthIndex = cursor.getColumnIndex(MediaStore.Images.ImageColumns.WIDTH);
                heightIndex = cursor.getColumnIndex(MediaStore.Images.ImageColumns.HEIGHT);
            }
            // 获取行数据
            String data = cursor.getString(dataIndex);
            long dateTaken = cursor.getLong(dateTakenIndex);
            int width = 0;
            int height = 0;
            if (widthIndex >= 0 && heightIndex >= 0) {
                width = cursor.getInt(widthIndex);
                height = cursor.getInt(heightIndex);
            } else {
                // API 16 之前, 宽高要手动获取
                Point size = getImageSize(data);
                width = size.x;
                height = size.y;
            }
            // 处理获取到的榜首行数据
            handleMediaRowData(data, dateTaken, width, height);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
        }
    }
    /**
      * 获取图片宽和高
      */
    private Point getImageSize(String imagePath) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(imagePath, options);
        return new Point(options.outWidth, options.outHeight);
    }
    /**
     * 处理获取到的一行数据
     */
    private void handleMediaRowData(String data, long dateTaken, int width, int height) {
        if (CommunityApplication.self.isComeBack()) {
            return;
        }
        if (checkScreenShot(data, dateTaken, width, height)) {
            Log.d(TAG, "ScreenShot: path = " + data + "; size = " + width + " * " + height
                    + "; date = " + dateTaken);
            if (mListener != null && !checkCallback(data)) {
                mListener.onShot(data);
            }
        } else {
            // 假如在调查区间媒体数据库有数据改动,又不契合截屏规矩,则输出到 log 待剖析
            Log.w(TAG, "Media content changed, but not screenshot: path = " + data
                    + "; size = " + width + " * " + height + "; date = " + dateTaken);
        }
    }
    /**
     * 判别指定的数据行是否契合截屏条件
     */
    private boolean checkScreenShot(String data, long dateTaken, int width, int height) {
        /*
         * 判别根据一: 时刻判别
         */
        // 假如加入数据库的时刻在开始监听之前, 或者与当时时刻相差大于10秒, 则以为当时没有截屏
        if (dateTaken < mStartListenTime || (System.currentTimeMillis() - dateTaken) > 10 * 1000) {
            return false;
        }
        /*
         * 判别根据二: 尺度判别
         */
        if (sScreenRealSize != null) {
            // 假如图片尺度超出屏幕, 则以为当时没有截屏
            if (!((width <= sScreenRealSize.x && height <= sScreenRealSize.y)
                    || (height <= sScreenRealSize.x && width <= sScreenRealSize.y))) {
                return false;
            }
        }
        /*
         * 判别根据三: 途径判别
         */
        if (TextUtils.isEmpty(data)) {
            return false;
        }
        data = data.toLowerCase();
        // 判别图片途径是否含有指定的关键字之一, 假如有, 则以为当时截屏了
        for (String keyWork : KEYWORDS) {
            if (data.contains(keyWork)) {
                return true;
            }
        }
        return false;
    }
    /**
     * 判别是否已回调过, 某些手机ROM截屏一次会宣布多次内容改动的告诉; <br/>
     * 删去一个图片也会发告诉, 同时避免删去图片时误将上一张契合截屏规矩的图片当做是当时截屏.
     */
    private boolean checkCallback(String imagePath) {
        if (sHasCallbackPaths.contains(imagePath)) {
            Log.d(TAG, "ScreenShot: imgPath has done"
                    + "; imagePath = " + imagePath);
            return true;
        }
        // 大概缓存15~20条记录便可
        if (sHasCallbackPaths.size() >= 20) {
            for (int i = 0; i < 5; i++) {
                sHasCallbackPaths.remove(0);
            }
        }
        sHasCallbackPaths.add(imagePath);
        return false;
    }
    /**
     * 获取屏幕分辨率
     */
    private Point getRealScreenSize() {
        Point screenSize = null;
        try {
            screenSize = new Point();
            WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
            if (windowManager != null) {
                Display defaultDisplay = windowManager.getDefaultDisplay();
                defaultDisplay.getRealSize(screenSize);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return screenSize;
    }
    /**
     * 设置截屏监听器
     */
    public void setListener(OnScreenShotListener listener) {
        mListener = listener;
    }
    public interface OnScreenShotListener {
        void onShot(String imagePath);
    }
    private static void assertInMainThread() {
        if (Looper.myLooper() != Looper.getMainLooper()) {
            StackTraceElement[] elements = Thread.currentThread().getStackTrace();
            String methodMsg = null;
            if (elements != null && elements.length >= 4) {
                methodMsg = elements[3].toString();
            }
            throw new IllegalStateException("Call the method must be in main thread: " + methodMsg);
        }
    }
    /**
     * 媒体内容调查者(调查媒体数据库的改动)
     */
    private class MediaContentObserver extends ContentObserver {
        private Uri mContentUri;
        public MediaContentObserver(Uri contentUri, Handler handler) {
            super(handler);
            mContentUri = contentUri;
        }
        @Override
        public void onChange(boolean selfChange) {
            LogUtils.e("ScreenShotListenManager  MediaContentObserver  onChange");
            super.onChange(selfChange);
            handleMediaContentChange(mContentUri);
        }
    }
}

Android Q版别无法检测到媒体数据库改变的问题

Android Q(10) ContentObserver 不回调 onChange这篇文章供给的解决方法:在Android Q版别上调用注册媒体数据库监听的方法registerContentObserver时传入 notifyForDescendants参数值改为 true,Android Q之前的版别仍然传入 false。

于是查看了一下文档上关于参数notifyForDescendants的介绍,大致内容如下:

* @param notifyForDescendants When false, the observer will be notified
 *            whenever a change occurs to the exact URI specified by
 *            <code>uri</code> or to one of the URI's ancestors in the path
 *            hierarchy. When true, the observer will also be notified
 *            whenever a change occurs to the URI's descendants in the path
 *            hierarchy.

Descendants:后嗣、子孙

假如值为false,则只需指定的URI或途径层次结构中URI的祖先之一发生改变,就会告诉调查者。 假如为true,则每逢途径层次结构中URI的子孙发生更改时,也会告诉调查者。

参阅:

www.jianshu.com/p/a7fab8faa…