I want to detect screenshots inside a specific activity inside my app. I decided to use this ScreenShot Detector library but unfortunately this does not detect any screenshots for some reason. I added some code to my activity to check if all the required permissions are grandent and it still doesn't work.
My Activity:
public class UserProfileActivity extends AppCompatActivity implements ScreenshotDetectionDelegate.ScreenshotDetectionListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_profile);
ActivityResultLauncher<String> requestPermissionLauncher =
registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
if (isGranted) {
Toast.makeText(this, "Permission ok", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Permission req", Toast.LENGTH_SHORT).show();
// Explain to the user that the feature is unavailable because the
// features requires a permission that the user has denied. At the
// same time, respect the user's decision. Don't link to system
// settings in an effort to convince the user to change their
// decision.
}
});
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
// You can use the API that requires the permission.
requestPermissionLauncher.launch(Manifest.permission.READ_EXTERNAL_STORAGE);
}
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
CustomIntent.customType(UserProfileActivity.this, "right-to-left");
}
@Override
protected void onStart() {
super.onStart();
screenshotDetectionDelegate.startScreenshotDetection();
}
@Override
protected void onStop() {
super.onStop();
screenshotDetectionDelegate.stopScreenshotDetection();
}
@Override
protected void onResume() {
super.onResume();
screenshotDetectionDelegate.startScreenshotDetection();
}
@Override
public void onScreenCaptured(@NonNull String s) {
sendNotificationToUser("screenshot");
Toast.makeText(UserProfileActivity.this, "Profile Screenshotted", Toast.LENGTH_SHORT).show();
}
@Override
public void onScreenCapturedWithDeniedPermission() {
Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show();
}
}