How to Prevent Screenshot Or Screen Recorder in Android?
In some situations, we don’t want to allow to take screenshots or screen recordings of our android application. Here we are going to explain how to prevent Android from taking a screenshot or screen recording when the app goes to the background. Generally, when we take a screenshot, we will see a Screen Capture notification in the notification bar and you can see that screenshot in the Gallery app if you click that notification. And some of the android application we can’t take a screenshot of the visible screen because of the screen secured by the developer. In this situation, we’ll see the message in the notification bar or in the Toast over the screen. Some of the payment transfer apps or course-containing apps have this feature. In this article, we’re going to create a simple project to demonstrate how this feature works. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. We will create a simple TextView inside the activity_main.xml file.
In the MainActivity.java file simply add the following code and it will prevent taking Screenshot in Android App.
Below is the complete code for the MainActivity.java file.
Method 1 is only appropriate for a single activity but what’s the solution for block screenshot for all the activities. We are going to discuss this in method 2. First of all, make a Custom Application class and add a registerActivityLifecycleCallbacks. Then register it in your manifest.
Step 1: Here write the given code in the MyApplicationContext.java file
Step 2: Register it in the manifest file like the following
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:name=".MyApplicationContext" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
Here you can see it clearly it neither allows taking screenshots nor screen recordings as when we try to take screen recordings it turns black.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.