The company's project because of security needs, to a specific page needs to listen to whether the user screenshot.
Simple search, there are few problems in this area, no way, can only toss themselves.
Now think of three ideas:
1. Listening and broadcasting
Of course, the premise is that the system sends a broadcast while the screen is being intercepted, but not.
2. Monitor button
Android Phone Press "Power key + volume minus" will be screenshots, in addition, most of the mobile Phone status bar dropdown page will also have a screenshot button. Unfortunately, listening to these two operations is not a pleasant thing ~ ~.
3, monitor the change of the picture in the phone
Started thinking of Mediastore this class, you can get all the pictures on the phone, listen to the number of pictures at intervals. It seemed like a good idea until I met Contentobserver on the corner.
From the name you can tell that it is a content observer. By registering the contentobserver with ContentProvider, the data can be monitored.
[Java]View PlainCopy
- Public class Screenshotcontentobserver extends Contentobserver {
- private Context Mcontext;
- private int imagenum;
- private static screenshotcontentobserver instance;
- Private Screenshotcontentobserver (context context) {
- super (null);
- Mcontext = context;
- }
- public static void Startobserve () {
- if (instance = = null) {
- Instance = New Screenshotcontentobserver (Facade.context ());
- }
- Instance.register ();
- }
- public static void Stopobserve () {
- Instance.unregister ();
- }
- private Void Register () {
- Mcontext.getcontentresolver (). Registercontentobserver (MediaStore.Images.Media.EXTERNAL_CONTENT_URI, false, This );
- }
- private void unregister () {
- Mcontext.getcontentresolver (). Unregistercontentobserver (this);
- }
- @Override
- public void OnChange (boolean selfchange) {
- Super.onchange (Selfchange);
- string[] Columns = {
- MediaStore.MediaColumns.DATE_ADDED,
- MediaStore.MediaColumns.DATA,
- };
- cursor cursor = null;
- try {
- cursor = Mcontext.getcontentresolver (). Query (
- MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
- Columns
- NULL,
- NULL,
- MediaStore.MediaColumns.DATE_MODIFIED + "desc");
- if (cursor = = null) {
- return;
- }
- int count = Cursor.getcount ();
- if (imagenum = = 0) {
- Imagenum = count;
- } Else if (imagenum >= count) {
- return;
- }
- Imagenum = count;
- if (Cursor.movetofirst ()) {
- String FilePath = cursor.getstring (Cursor.getcolumnindex (MediaStore.MediaColumns.DATA));
- Long addtime = Cursor.getlong (Cursor.getcolumnindex (MediaStore.MediaColumns.DATE_ADDED));
- if (Matchaddtime (addtime) && Matchpath (FilePath) && matchsize (FilePath)) {
- DoReport (FilePath);
- }
- }
- } catch (Exception e) {
- E.printstacktrace ();
- } finally {
- if (cursor! = null) {
- try {
- Cursor.close ();
- } catch (Exception e) {
- E.printstacktrace ();
- }
- }
- }
- }
- /**
- * Add time and current time not more than 1.5s, most of the time not more than 1s.
- *
- * @param addtime Picture Add time, units: seconds
- */
- Private Boolean matchaddtime (long addtime) {
- return System.currenttimemillis ()-Addtime * < 1500;
- }
- /**
- * size is not larger than screen size (found 360 odd cool phones can be clipped on the screenshot)
- */
- Private Boolean matchsize (String filePath) {
- Point size = Util.getscreenwidthandheight (Mcontext); //Get screen size
- Bitmapfactory.options Options = new Bitmapfactory.options ();
- Options.injustdecodebounds = true;
- Bitmapfactory.decodefile (FilePath, Options);
- return size.x >= options.outwidth && size.y >= options.outheight;
- }
- /**
- * Survey of mobile phone screenshot picture path with screenshot
- */
- Private Boolean Matchpath (String filePath) {
- String lower = Filepath.tolowercase ();
- return Lower.contains ("screenshot");
- }
- private void DoReport (String filePath) {
- //Delete screenshot
- File File = new file (FilePath);
- File.delete ();
- //todo:
- }
- }
Registration and anti-registration of listeners via register () and unregister () two static methods. It is recommended that you register in the OnStart () method and reverse-register in the OnStop () method, because the screenshot does not cause changes in the current page life cycle.
In the onchange () callback method, through the query, get the recently added picture, from the creation time, size, path 3 aspects to match, to determine whether it is a screenshot picture.
- Time to create: Most of the time, the screen picture is created and the current system time does not exceed 1000ms
- Image size: Most mobile phone screenshots, save the image directly, so the size and screen size is the same. But some phones, such as 360 odd cool phones, cut the screen and allow users to crop. So the size of the picture is more than the size of the screen.
- Picture path: Currently, the screenshot path for most phones contains "screenshot" and no exceptions have been found
After the match is successful, you can do what you want to do in DoReport.
Monitor the Android system screenshot