Android Question Maintain Activity in Brackground

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Hello,

I have a WebView that runs a player in an app. When this app is paused, the streaming/sound played by webview continues, what is great... but if I actively close the activity in background (or it's killed by the system), the sound stops. Is there any way to maintain the activity running in background (webview in activity is running!), like we do with a service?
 

Harris

Expert
Licensed User
Longtime User
Somehow - use a service. They can be set to keep running (or quickly restarted) - even if the activity is killed.
Yes, Webview is an activity component. Does streaming sound need to be part of the webview?

Just reaching here...
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Somehow - use a service. They can be set to keep running (or quickly restarted) - even if the activity is killed.
Yes, Webview is an activity component. Does streaming sound need to be part of the webview?

Just reaching here...
Some sites doesn't expose the streaming... only a server side method to call it (http://site.com/play.php)
Then, there is no other way to play the audio (only if there is someway to simulate a page/site running like in a webview). That's because isn't possible in many cases to use a service (at least if there is an object instead of a view that does exactly the webview job)
 
Last edited:
Upvote 0

Harris

Expert
Licensed User
Longtime User
Check this out... (IsPaused may help)...

https://www.b4x.com/android/forum/threads/service-modules.7542/#content

Accessing other modules

Process global objects are public and can be accessed from other modules.
Using CallSub method you can also call a sub in a different module.
It is however limited to non-paused modules. This means that one activity can never access a sub of a different activity as there could only be one running activity.
However an activity can access a running service and a service can access a running activity.
Note that if the target component is paused then an empty string returns.
No exception is thrown.
You can use IsPause to check if the target module is paused.

For example if a service has downloaded some new information it can call:
Code:
CallSub(Main, "RefreshData")
If the Main activity is running it will fetch the data from the service process global variables and will update the display.
It is also possible to pass the new information to the activity sub. However it is better to keep the information as a process global variable. This allows the activity to call RefreshData whenever it want and fetch the information (as the activity might be paused when the new information arrived).

Note that it is not possible to use CallSub to access subs of a Code module.
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User

There is a really interesting behavior with WebViews playing streaming in paused Activities... the audio continues playing (!!!!). The sound should stop, as the Activity containing the WebView is paused, but this doesn't occurs - the streaming/sound continues, even when the parent activity is paused. BUT there is a difference between a paused Activity and a closed activity - when we pause, the streaming continues... when we close (swept of the screen), the sound stops! But even if the Activity is stopped/swept , the "ispaused" method returns true (whats is logic because false means open on the screen...). So, the question: how can we detected if the Activity is really paused or is killed ?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You are confusing different things so it is difficult to answer your questions. I recommend you to watch the B4A life cycle video tutorial: https://www.b4x.com/etp.html

Don't rely on WebView to continue to work while the app is in the background. The only way for an app to do something in the background is with a foreground service.
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User

Yes, I know that the reliable way to do background jobs is to use a service - the best way is to use MediaPlayerStreaming in a service, but to use this solution It's needed to get the streaming link, which sometimes we don't know. Running the website like in a Browser is an easy solution, but the only way that I know to do this is using a WebView...
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…