11

I am new to android development.

I am making an application that will react when screenshot of android phone is taken. I have heard that android allows such actions can be detected by Broadcast Receivers , so I have gone through android developer documentation, here.

I think the framework developers forgot or didn't implement a screenshot broadcast code, because they haven't listed this action in their documentation.

Is there any other way I can listen to screenshot taking action ?

Bqin1
  • 467
  • 1
  • 9
  • 19
droidev
  • 7,352
  • 11
  • 62
  • 94
  • Hey, have you tested [this solution](http://stackoverflow.com/a/14951594/518179) using FileObserver? – Renan Ferrari Jul 11 '15 at 20:14
  • @renam nope.. I'll check it and inform you soon – droidev Jul 11 '15 at 20:38
  • 1
    Just FYI I think file observers will work but some versions are buggy with it like android 6.0 https://stackoverflow.com/questions/36237314/fileobserver-and-contentobserver-not-working-in-android-marshmallow – Bqin1 Aug 09 '17 at 03:16

1 Answers1

3

You can detect ScreenShot taken event using ContentObserver. I am using it in one of my projects.

ScreenShotContentObserver.java

public abstract class ScreenShotContentObserver extends ContentObserver {

    private Context context;
    private boolean isFromEdit = false;
    private String previousPath;

    public ScreenShotContentObserver(Handler handler, Context context) {
        super(handler);
        this.context = context;
    }

    @Override
    public boolean deliverSelfNotifications() {
        return super.deliverSelfNotifications();
    }

    @Override
    public void onChange(boolean selfChange) {
        super.onChange(selfChange);
    }

    @Override
    public void onChange(boolean selfChange, Uri uri) {
        Cursor cursor = null;
        try {
            cursor = context.getContentResolver().query(uri, new String[]{
                    MediaStore.Images.Media.DISPLAY_NAME,
                    MediaStore.Images.Media.DATA
            }, null, null, null);
            if (cursor != null && cursor.moveToLast()) {
                int displayNameColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME);
                int dataColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
                String fileName = cursor.getString(displayNameColumnIndex);
                String path = cursor.getString(dataColumnIndex);
                if (new File(path).lastModified() >= System.currentTimeMillis() - 10000) {
                    if (isScreenshot(path) && !isFromEdit && !(previousPath != null && previousPath.equals(path))) {
                        onScreenShot(path, fileName);
                    }
                    previousPath = path;
                    isFromEdit = false;
                } else {
                    cursor.close();
                    return;
                }
            }
        } catch (Throwable t) {
            isFromEdit = true;
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        super.onChange(selfChange, uri);
    }

    private boolean isScreenshot(String path) {
        return path != null && path.toLowerCase().contains("screenshot");
    }

    protected abstract void onScreenShot(String path, String fileName);

}

And use this class in your activity :-

public class MainActivity extends AppCompatActivity {

    private ScreenShotContentObserver screenShotContentObserver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        HandlerThread handlerThread = new HandlerThread("content_observer");
        handlerThread.start();
        final Handler handler = new Handler(handlerThread.getLooper()) {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
            }
        };

        screenShotContentObserver = new ScreenShotContentObserver(handler, this) {
            @Override
            protected void onScreenShot(String path, String fileName) {
                File file = new File(path); //this is the file of screenshot image
            }
        };

    }

    @Override
    public void onResume() {
        super.onResume();

        getContentResolver().registerContentObserver(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                true,
                screenShotContentObserver
        );
    }

    @Override
    public void onPause() {
        super.onPause();

        try {
            getContentResolver().unregisterContentObserver(screenShotContentObserver);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        try {
            getContentResolver().unregisterContentObserver(screenShotContentObserver);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

Don't forget to stop Observer in onPause or onDestroy methods of an Activity

Mahesh Babariya
  • 4,560
  • 6
  • 39
  • 54
  • MainActivity passes `handler` to the constructor of ScreenShotContentObserver. But, where does MainActivity get `handler` from? – Brian Davis Feb 05 '19 at 21:44
  • detecting a screenshot only by looking for "screenshot" part in filename is not good – G. Kh. Feb 11 '19 at 09:51