Android Question Service - Activity Interaction

DaveB4A

Member
Licensed User
Longtime User
I am attempting to simulate a very simple function - like that of the default Android "Clock / Timer" function. If you open the "Clock", select "Timer" and set an amount of time - you can return to the HOME screen (or begin working in any other application) and, when the time on the timer expires, an activity screen will appear (and interrupt you, regardless of what application you are currently using in the foreground) announcing the expiration of the timer.

My B4A app launches a very simple screen (allowing the user to choose the number of minutes to delay) and then sets a corresponding service to launch on a delay using "StartServiceAt". When the service launches, I use "CallSubDelayed" to launch the activity screen to notify the user that the timer has expired.

The application works fine - as long as my program is still "active" at the time the service launches. If my application is in the "background" at that point (say, after pressing the HOME key after setting the alarm) - B4A displays a message:

B4X:
sending message to waiting queue (CallSubDelayed - DisplayAlarmScreen)

After reading the documentation, I assume that this is what is to be expected, based on:

B4X:
http://www.b4x.com/android/forum/threads/using-callsubdelayed-to-interact-between-activities-and-services.18691/
 
- If the target module is an activity:

 - If the application is in the background (can happen when a service calls an activity) then the message will be stored in a special message queue. In this case the sub will be called when the target activity becomes visible. The sub will be called before Activity_Resume.

My question is... How do I get B4A to "force" my application screen to the foreground when the service starts?
 

DaveB4A

Member
Licensed User
Longtime User
Erel, thank you for the info. I added that call as follows:

B4X:
Sub Service_Start (StartingIntent As Intent)
    StartActivity(DisplayAlarm)
    CallSubDelayed(DisplayAlarm, "DisplayAlarmScreen")
End Sub

The application seems to respond as desired now. However, I'm trying (as a brand new user) to understand why this call was necessary.

The B4A book states:

CallSubDelayed is a combination of StartActivity, StartService and CallSub.
Unlike CallSub (which only works with currently running components), CallSubDelayed will first start the target component if needed.

Note: If you call an Activity while the whole application is in the background (no visible activities), the sub will be executed once the target activity is resumed.

But, in the tutorial, "Using CallSubDelayed to interact between activities and services" it states:

Just to make it clear, you do not need to call StartActivity or StartService when you use CallSubDelayed. CallSubDelayed is the recommended method for interaction between activities and services.

Again the code is now working fine, I'm just trying to make sure I understand "why". :)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This note is important and answers your question:
Note: If you call an Activity while the whole application is in the background (no visible activities), the sub will be executed once the target activity is resumed.

In most cases services do not start activities if the app is in the background. It is a confusing interface. The user is doing something and then an activity jumps to the foreground. This is why CallSubDelayed message is kept in a special queue in this case and waits for the activity to resume.
 
Upvote 0
Top