bgs-core icon indicating copy to clipboard operation
bgs-core copied to clipboard

startForeground & Keeping WebViews Alive

Open lylepratt opened this issue 11 years ago • 11 comments

I'm interested in using this plugin to create a "dummy" service as described in this StackOverflow post in order to prevent a WebView from being killed (so javascript continues to execute): http://stackoverflow.com/questions/12226703/backgrounding-phonegap-and-the-relationship-between-activity-process-and-servi

It looks like in order for the above to work, I have to use startForeground which requires onStartCommand. Basically my purpose for this is because I'm using Sip.JS & WebRTC to create a VoIP phone and need to be able to continue to receive calls in the background (so can't have the webview getting killed).

It looks like all I need to do is implement onStartCommand and startForeground to accomplish this, but I was curious if you had any additional thoughts or experience on keeping WebViews alive.

Thanks, Lyle

lylepratt avatar Oct 11 '14 17:10 lylepratt

Example of startForeground being used in a Cordova plugin: https://github.com/christocracy/cordova-plugin-background-geolocation/blob/master/src/android/LocationUpdateService.java#L164

lylepratt avatar Oct 11 '14 17:10 lylepratt

@Red-Folder I created a plugin based on your sample that does what I describe above: https://github.com/BetterVoice/bv-bgs-foreground

Works great.

lylepratt avatar Oct 14 '14 18:10 lylepratt

Good job. Well done.

Have you thought about merging this code into the bgs-core repository?

Red-Folder avatar Oct 27 '14 20:10 Red-Folder

Enhancement: Add startForeground as an option.

Some notes on startForeground here -> http://developer.android.com/reference/android/app/Service.html#onStart(android.content.Intent,int)

Note that I don't specifically think it is a valid scenario to use the service to keep the app open - that's just not how the Android lifecycle is designed (from my understanding)

Red-Folder avatar Mar 17 '15 12:03 Red-Folder

I've been using startForeground as a method to keep the app alive for a few months now. You're correct that it is not designed for keeping the app open, but it does decrease the likelyhood that the app will be killed due to memory pressure because the OS assumes the user is using the functionality actively in the foreground.

My particular use case is for a VoIP app that uses webRTC. I display a notification that shows the current VoIP status, call status, etc, so it works nicely for me.

lylepratt avatar Mar 17 '15 13:03 lylepratt

Merging in of old repo issue -> https://github.com/Red-Folder/Cordova-Plugin-BackgroundService/issues/40

Red-Folder avatar Mar 21 '15 08:03 Red-Folder

(Notes to self)

For a custom Notification, see http://developer.android.com/guide/topics/ui/notifiers/notifications.html (scroll to the bottom and Custom Notification Layouts)

Probably best in first incarnation to follow Android standard Notification setup. Then an additional enhancement to look at custom.

Gut feel is that you wouldn't be able to include a webview into a custom notification (bit of a shame).

Red-Folder avatar Mar 25 '15 12:03 Red-Folder

@lylepratt On Galaxy S6 Edge, I don't know if it is a Samsung issue that even after creating a background service STICKY and acquiring the partial wakelock, the app once killed by the process manager never restarts. It restarts only once the phone is rebooted. The same does not happens on stock android though. Thanks to you for providing a "foreground" service option. After using this in combination with "partial wakelock" i have ensured that even on galaxy S6 edge the service keeps on running without getting killed.

charmingarpit avatar Jan 11 '16 04:01 charmingarpit

@lylepratt Also i would like to ask you, if there is any way to make the service run as a foreground service but without any notification on the notification area?

charmingarpit avatar Jan 11 '16 04:01 charmingarpit

I think the core requirement of a "foreground" service is that it HAS to display a notification. Unfortunately it won't be possible to run the process in the "foreground" without the notification.

Relevant stackoverflow post: http://stackoverflow.com/questions/10962418/startforeground-without-showing-notification On Jan 10, 2016 10:22 PM, "charmingarpit" [email protected] wrote:

@lylepratt https://github.com/lylepratt Also i would like to ask you, if there is any way to make the service run as a foreground service but without any notification on the notification area?

— Reply to this email directly or view it on GitHub https://github.com/Red-Folder/bgs-core/issues/17#issuecomment-170427733.

lylepratt avatar Jan 11 '16 04:01 lylepratt

@lylepratt ok. thanks for the help. In my service I had to additionally take wakelock as described in https://github.com/Red-Folder/Cordova-Plugin-BackgroundService/issues/52 by vista-54. However, I see that this is eating up a lot of battery. Is there any alternative?

charmingarpit avatar Jan 11 '16 05:01 charmingarpit