0

I have an app with an activity that records Video. The activities view has a start button and a timer graphic/textview. So when the record button is pressed it starts recording video, changes the button to a stop recording icon and starts updating the timer textview every second. When the power button is pressed and the screen turns off the activity is stopped and recording has stopped. When I turn the screen back on the activity is reset to start recording again.

The question is how do I enable the activity that is attached to the view to remain active once the screen is off/locked.

I've read about creating a service but can you attach a view to a service such that the time and stop recording UI elements are properly maintained when the screen is active again; i.e. if I start recording the timer textview starts incrementing every second, if after 5 seconds recording I lock the screen and 5 second later I activate the screen can the activity be kept alive so that the timer etc. will have kept running in the background and now display 15 seconds.

Any help much appreciated

Diplonics
  • 435
  • 1
  • 4
  • 12

2 Answers2

1

Try using an IntentService and perform long running operations in a separate thread if required. Use Messenger, EventBus, Broadcast receiver to update your UI from the service. You can also set Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND) in the onHandleIntent() method of your intent service.

kamal2305
  • 671
  • 4
  • 11
  • Thanks for this, loads of interesting bits to lookup here so a good set of key words to google, especially the IntentService. I found this [link](http://stackoverflow.com/questions/2463175/how-to-have-android-service-communicate-with-activity) based on your hints which is giving plenty of reading to advance what I want to do. – Diplonics Mar 20 '15 at 15:00
  • @Diplonics If it helped please vote up the answer :) – kamal2305 Mar 21 '15 at 10:06
1

You'll likely need to request a wake lock to ensure the CPU doesn't go to sleep. See Android's documentation on Wake Locks.

You'll also want to request foreground priority in your service to make sure Android doesn't kill it (note: it still will kill it but only in the most extreme of conditions). See Android's documentation on Running a Service in the Foreground.

Austin Hanson
  • 21,820
  • 6
  • 35
  • 41
  • Thanks for this. I had the wake-lock set as follows: which forced the device to stay awake duration but it kills the battery. However even with this set if I press the power button to lock the device and a recording has been started then it just halted the recording. – Diplonics Mar 20 '15 at 14:57