0

I'm running into an issue and I would appreciate some guidance. I'm writing a small Android program in Android Studio that uses MediaProjection to grab a screenshot of the whole screen and then I want to pass that screenshot as a Bitmap to my System Overlay (based on the chatheads example). When the MediaProjection runs and the Imagereader creates the bitmaps, I'm passing the bitmap into a class global variable so I can pass it into my System Overlay service and display it. The problem I'm running into is if I "tab out" to a different app, my methods stop working from the MainActivity. When my app is in focus, everything works well, when a different app is focused, the system overlay contracts and expands like it should, but the screenshots are not being taken anymore. My understand is its because my app becomes "asleep" since it's not in focus. Could anyone recommend a solution? Can I momentarily awake my mainActivity so it can take a screenshot? Can I keep my activity from falling asleep on app switch? Can I bring focus to my app, take the screenshot and then switch focus to the previous app? Any other ways?

My code can be found here: https://github.com/asheron21/FacebookLikeChathead

MainActivity:https://github.com/asheron21/FacebookLikeChathead/blob/master/app/src/main/java/com/example/chatheads/MainActivity.java

Service: https://github.com/asheron21/FacebookLikeChathead/blob/master/app/src/main/java/com/example/chatheads/ChatHeadService.java

Global Variables:https://github.com/asheron21/FacebookLikeChathead/blob/master/app/src/main/java/com/example/chatheads/Globals.java

Thanks!

Asheron
  • 33
  • 4
  • You might find [this](https://stackoverflow.com/a/31451945/2737933) helpful. I looked at your code. It looks like what I'd expect except why not extend `IntentService`? I ask only because that's what I did to keep a background task running. – DSlomer64 Jul 08 '18 at 21:08
  • With `service` you have to worry about threads, starting and stopping service, handling `run` and probably more. With `IntentService`, as the link above says, you "fire and forget" since `IntentService` takes care of all that for you. Maybe just for future reference; not suggesting rewrite. The fact that your code runs fine while app is focused probably means it won't take much to fix the fact that it quits when unfocused. I've been studying the code for half an hour. No clue yet. – DSlomer64 Jul 08 '18 at 21:52
  • @DSlomer64 Wow, thank you for pointing out the IntentService for me, it seems like a good fit. Now to just figure out how to implement it. I'm thinking that I might have to rewrite for a couple of reasons, one to have everything be a bit more organized and two, I think I need to transfer MediaProjection screenshotting code to the Service instead of the Main Activity. You've mentioned you've used IntentServices in your app, can you suggest any good tutorials or examples on it? – Asheron Jul 08 '18 at 22:40
  • I found this sample code for a MediaProjection being run from Service, although it has a lot more stuff than I need. It's going to take a little while to sift through all that code. https://github.com/saki4510t/ScreenRecordingSample/tree/master/app/src/main/java/com/serenegiant – Asheron Jul 09 '18 at 00:35

0 Answers0