Monitor the Android system screenshot

Source: Internet
Author: User
Tags add time

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  
  1. Public class Screenshotcontentobserver extends Contentobserver {
  2. private Context Mcontext;
  3. private int imagenum;
  4. private static screenshotcontentobserver instance;
  5. Private Screenshotcontentobserver (context context) {
  6. super (null);
  7. Mcontext = context;
  8. }
  9. public static void Startobserve () {
  10. if (instance = = null) {
  11. Instance = New Screenshotcontentobserver (Facade.context ());
  12. }
  13. Instance.register ();
  14. }
  15. public static void Stopobserve () {
  16. Instance.unregister ();
  17. }
  18. private Void Register () {
  19. Mcontext.getcontentresolver (). Registercontentobserver (MediaStore.Images.Media.EXTERNAL_CONTENT_URI, false,  This );
  20. }
  21. private void unregister () {
  22. Mcontext.getcontentresolver (). Unregistercontentobserver (this);
  23. }
  24. @Override
  25. public void OnChange (boolean selfchange) {
  26. Super.onchange (Selfchange);
  27. string[] Columns = {
  28. MediaStore.MediaColumns.DATE_ADDED,
  29. MediaStore.MediaColumns.DATA,
  30. };
  31. cursor cursor = null;
  32. try {
  33. cursor = Mcontext.getcontentresolver (). Query (
  34. MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
  35. Columns
  36. NULL,
  37. NULL,
  38. MediaStore.MediaColumns.DATE_MODIFIED + "desc");
  39. if (cursor = = null) {
  40. return;
  41. }
  42. int count = Cursor.getcount ();
  43. if (imagenum = = 0) {
  44. Imagenum = count;
  45. } Else if (imagenum >= count) {
  46. return;
  47. }
  48. Imagenum = count;
  49. if (Cursor.movetofirst ()) {
  50. String FilePath = cursor.getstring (Cursor.getcolumnindex (MediaStore.MediaColumns.DATA));
  51. Long addtime = Cursor.getlong (Cursor.getcolumnindex (MediaStore.MediaColumns.DATE_ADDED));
  52. if (Matchaddtime (addtime) && Matchpath (FilePath) && matchsize (FilePath)) {
  53. DoReport (FilePath);
  54. }
  55. }
  56. } catch (Exception e) {
  57. E.printstacktrace ();
  58. } finally {
  59. if (cursor! = null) {
  60. try {
  61. Cursor.close ();
  62. } catch (Exception e) {
  63. E.printstacktrace ();
  64. }
  65. }
  66. }
  67. }
  68. /** 
  69. * Add time and current time not more than 1.5s, most of the time not more than 1s.
  70. *
  71. * @param addtime Picture Add time, units: seconds
  72. */
  73. Private Boolean matchaddtime (long addtime) {
  74. return System.currenttimemillis ()-Addtime * < 1500;
  75. }
  76. /** 
  77. * size is not larger than screen size (found 360 odd cool phones can be clipped on the screenshot)
  78. */
  79. Private Boolean matchsize (String filePath) {
  80. Point size = Util.getscreenwidthandheight (Mcontext); //Get screen size
  81. Bitmapfactory.options Options = new Bitmapfactory.options ();
  82. Options.injustdecodebounds = true;
  83. Bitmapfactory.decodefile (FilePath, Options);
  84. return size.x >= options.outwidth && size.y >= options.outheight;
  85. }
  86. /** 
  87. * Survey of mobile phone screenshot picture path with screenshot
  88. */
  89. Private Boolean Matchpath (String filePath) {
  90. String lower = Filepath.tolowercase ();
  91. return Lower.contains ("screenshot");
  92. }
  93. private void DoReport (String filePath) {
  94. //Delete screenshot
  95. File File = new file (FilePath);
  96. File.delete ();
  97. //todo:
  98. }
  99. }


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.