Android Question [SOLVED] Detect when users take device screenshots? (Android 14)

Greg Breinholt

Member
Licensed User
Longtime User
Has anyone managed to implement the Android 14 screenshot detection?
I would like to hide my app's floating UI when a screenshot is taken.

https://developer.android.com/about/versions/14/features/screenshot-detection#java
This Java part doesn't compile and I'm not sure how to get B4A to register this kind of callback:

Java:
final Activity.ScreenCaptureCallback screenCaptureCallback =
    new Activity.ScreenCaptureCallback() {
        @Override
        public void onScreenCaptured() {
            // Add logic to take action in your app.
        }
    };



Error shown:

B4X:
B4A Version: 12.80
Parsing code.    (0.00s)
    Java Version: 11
Building folders structure.    (0.01s)
Compiling code.    (0.01s)
Compiling layouts code.    (0.00s)
Organizing libraries.    (0.00s)
    (AndroidX SDK)
Compiling resources    (0.12s)
Linking resources    (0.13s)
Compiling generated Java code.    Error
B4A line: 19
End Sub
src\b4a\example\main.java:405: error: cannot find symbol
    final Activity.ScreenCaptureCallback screenCaptureCallback =
                  ^
  symbol:   class ScreenCaptureCallback
  location: class Activity
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
Where is the exampleproject showing the error?
Have you added the code with inlinejava?
Upload the project
 
Upvote 0

Greg Breinholt

Member
Licensed User
Longtime User
Yes, added as inline Java... and sorry, I was being hopeful that someone had dealt with this issue or something similar and knew the quick way to solve this ;-)

Attached is an empty sample project with the inline Java that I'm having issues connecting to B4A (the Java code is from the Google dev site).



EDIT: This sample project works. Need to update the Configure Path menu item to point to Android 14 jar.
 

Attachments

  • Screenshot Test.zip
    107.8 KB · Views: 18
Last edited:
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
needs 34 ...
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
but do you have the 34's android.jar? you can manipulate the target sdk.
 
Upvote 0

Greg Breinholt

Member
Licensed User
Longtime User
Not sure I understand.. but if I comment out the offending code, I can compile with a target 34. Then it will run in a Android 14 emulator with no issues.
I checked and I have the Android 14 SDK installed.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
i'm sorry, but what you're saying makes no sense to me. if you comment out the "offending code"
- presumably, the part that generates the Activity.ScreenCaptureCallback not found error - the remaining
code could not possibly compile since it refers (twice) to a variable that has been commented out (and,
therefore, cannot be found). without the callback, nothing works.

in any case, your example compiles and runs for me. see attached. i've also attached an image of
what happens if you comment out the code which causes the compile error.
 

Attachments

  • screencaptureex.png
    screencaptureex.png
    23.5 KB · Views: 28
  • commented.png
    commented.png
    40.7 KB · Views: 28
Upvote 0

Greg Breinholt

Member
Licensed User
Longtime User
First, thank you for you help!

Note that I was disabling all the inline Java just to see there was nothing else wrong with the code ;-)

Linking to the right Android14 jar (from the Configure Paths menu item) seems to have fixed it. Works as expected now (though need to run on actual hardware and not the emulator to see result). I updated the initial post with code that works and marked this as solved.

My only remaining issue is that the callback happens AFTER the screenshot is taken, and I really wanted to be notified before.
Ah well, might have to wait for Android 15 implement this ;-)

Once again, thank you!
 
Upvote 0
Top