Search code examples
androidscreenshotwallpaper

Android - Take a "Screenshot" of an App Running in the Background


I have read plenty of questions that deal with an app taking screenshot of the current users screen while the app runs in the background, but have not seen many articles that deal with the app taking a "screenshot" of itself while it runs in the background.

I would like this to be able to be done without having to root the phone in any way as I would like the app I make be available to everyone(rooting is probably not involved in this solution, but just throwing that out there).

The end goal of the app is for the app be able to take a screenshot of itself and save that screenshot as the users background wallpaper. There are several other features I would like to add, but I would just want to know, is this even possible? If this is, could anyone show me some starter code or link me to some?If this question has been asked before, please let me know, otherwise, any and all help is appreciated. Thank you!


Solution

  • Firstly, I am not really sure if I understand your requirement correctly. Ideally when the app is in background, its state when it went to the background and while it is in background will remain the same. So you can take a screenshot of your app, when the app is just going in background, which can be handled in your onPause() of the activity.

    To take screenshot of your app, place the entire layout in a ViewGroup and then use the following code.

        parentLayout = (RelativeLayout)findViewById(R.id.parentLayout);
        parentLayout.setDrawingCacheEnabled(true);
        parentLayout.buildDrawingCache();
    

    And when you want to take a screenshot, get the screenshot in the Bitmap by using

        bitmap = parentLayout.getDrawingCache();
    

    The above line should be in your onPause() thats when the app goes in background. Or you could also trigger it when your app is in background, and see if it is captured. I'm not really sure how it will work in the latter scenario.