The Screenshot Service Extension is able to capture a screenshot while your app is not running.
Minimum API level is 21 (Android 5).
For details, example projects and documentation see here https://puravidaapps.com/screenshotservice.php
What does the user see?
After starting the service the capturing app will display the following "ugly" dialog to the user depending on the Android version... (below Android 10 this dialog is less ugly )
The user must tap "Start Now" and a permanent notification will be displayed in the status bar for the user to start capturing a screenshot or shutdown the service.