0

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!

Varun Nair
  • 15
  • 3
  • What do you mean by _taking screenshot of itself while it runs in the background?_ If its running in the background, it wont be visible to the user. Then what screenshot are you talking about? Do you mean to take the screenshot of the phone at the current state while your app is in the background? – Antrromet Jul 16 '15 at 02:24
  • Not quite. I want to take a (so-called) "screenshot" of the app itself while it is in the background, not of the current state of the phone. – Varun Nair Jul 16 '15 at 02:39
  • What do you mean by _so called "screenshot"_? – Antrromet Jul 16 '15 at 02:55
  • I mean I want a picture of the app in its current state while I am elsewhere on my phone (on the home screen, checking email, etc..) to be taken by itself. Eventually I would like this picture to be stored as my wallpaper. @Antrromet – Varun Nair Jul 16 '15 at 03:02

1 Answers1

0

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.

Antrromet
  • 15,294
  • 10
  • 60
  • 75
  • Thanks for the help. As you mentioned, I would like the app to run primarily in the background, so if so, how would I go about doing so when it is in the background already? I have some knowledge of services, but am not sure if that's what you need to use in that instance. – Varun Nair Jul 16 '15 at 03:19
  • But as I mentioned, since the app state wont change when its in background, you can take the screenshot (at the moment) when the app goes to background (`onPause()`). And then use that image for wallpaper. You don't need a service for this. – Antrromet Jul 16 '15 at 03:22
  • But what if the app state would be changing? In that case would I need a service? – Varun Nair Jul 16 '15 at 03:24
  • Can you give an example when it'll change? – Antrromet Jul 16 '15 at 03:25
  • For example if it were to display weather conditions and then change the wallpaper of the phone appropriately, how would I do so? – Varun Nair Jul 16 '15 at 03:26
  • In that case you'd have to have a service running in the background which fetches the updated whether information from server periodically. And then you need to set that info to your view and then take the screenshot. – Antrromet Jul 16 '15 at 03:28
  • Ok, so the best way about doing this is creating a service that takes a screenshot of the app that displays changing information, using the code that you have provided above? Or does the screenshot code still need to be provided in the `onCreate` method? – Varun Nair Jul 16 '15 at 03:31