Android callback when trying to make a screenshot in my app

Viewed 208

I have an Android application with a video player with DRM. When I try to take a screenshot after the video player in my app is opened I see the message: "Can't take screenshot due to security policy". It is okay, but I want to customize this message or, in the best way, have a callback when the user tries to make a screenshot (for example, show him an alert dialog with my message).

How can I make it?

2 Answers

As this message came from OS, unfortunately you can not edit it. Also you can not have an event which triggers after this toast.

check this link

OR

  1. Gradle dependency Add it in your project-level build.gradle at the end of repositories:

    allprojects { repositories { ... maven { url 'https://jitpack.io' } } }

Add the dependency in yout app-level build.gradle:

implementation'com.github.rbague:ScreenshotCallback:v1.0'

Usage

Add this permission to AndroidManifest.xml and remeber to request it:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

Example of listening for screenshots while an activity is being shown:

    public class MainActivity extends AppCompatActivity {
    private ScreenshotObserver mObserver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        mObserver = new ScreenshotObserver() {
            @Override
            public void onScreenshotTaken(String path) {
                //Your code here
            }
        };
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        if (mObserver != null) {
            //Must be called or mObserver won't receive any calls
            mObserver.startListnening(); 
        }
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        if (mObserver != null) {
            mObserver.stopListening();
        }
    }
}
Related