Android Question StartActivity(Main) from Service

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi.
I have a service which runs also while the Main is paused. When the service detects a determinate situation, it should call a sub in the Main. It seems that, while a CallSub(Main,"SubToCall") (inside the Service), when Main is active, works fine, this is not true when Main is Paused.
Worse, it seems to me that adding:
If IsPaused(Main) then StartActivity(Main)
before CallSub, doesn't resolve. (tried also CallSubDelayed)
Anybody knows what I am missing? Thanks in advance.
 

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi, Erel. I posted the code, some messages before. I have a service that needs to call a Sub in the Main, while this latter is Paused. CallSub(delayed) and StartActivity seems not to resolve the issue. My example is rather simple. I already did any possible trial, at my level of knowledge, of course. Thanks.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
StartActivity sends a message to the internal message queue. When this message is processed then the activity is actually started.

If the activity was paused when the sub started then your CallSub call will never work.

Changing to CallSubDelayed does work as expected:

Before call
Sub Called ----------- OK
** Activity (main) Pause, UserClosed = false **
Before call
sending message to waiting queue (CallSubDelayed - SubToCall)
running waiting messages (1)
Sub Called ----------- OK

The sub is called right after the activity is created or resumed.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi. It is probably my misunderstanding, then. As a matter of fact I already verified that, as you said, after manual resuming, the sub is called (using CallSubDelayed). Nevertheless my problem is another and remains : I want that the service displays a Dialog, when an event inside the Service happens, but the user has not to manually reactivate the Activity.
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
As far I know, services can not display dialogs. So you could start a second activity (even a transparent activity) to show it
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi. I know that a service has no user interface. That's why I try to call the Main, where to display the dialog etc. Now, as I see, no CallSub (delayed or not) to Main Subs is processed if the Main activity is Paused. Inserting a StartActivity(Main) doesn't resolve: the Main activity remains paused (sincerely it sounds a little strange to me, this fact). Therefore, no user interface can be "automatically" displayed by the service: we must wait that the user activates manually the Main activity. Thanks.
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
Inserting a StartActivity(Main) doesn't resolve: the Main activity remains paused
This is absolutely not as it works by me : when I use StartActivity(Main), the Main is started and Resumed each time.
Can't the Main be hidden by the lockscreen ? If it is what you mean by "not displayed", you must search the forum for the SetShowWhenLocked function.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi, LemonIsDead (hope Orange is still alive). Your suggestion to use PhoneWakeState library resolves. As a matter of fact, the option KeepAlive may be used also to wake the phone, even with MainActivity paused. Probably this is a real "StartActivity(Main)" option... (or at least how I intended it).
The problem is that this option will prevent from further pausing, and this may arise battery issues. Having no way to check the PhoneWakeState, I added a variable in the Service to allow the Called sub to release the PhoneState with "ReleaseKeepAlive". Of course I have an ugly screen wake up, but this is a minor issue.
I attach the code in case someone is interested in this issue, or for further comments. As a matter of fact my solution is pretty empirical ..
Thanks again.
Giovanni
 

Attachments

  • TestService_2.zip
    6.8 KB · Views: 200
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
And, finally, just a CallSub (not delayed) can be used, after PhoneWakeState.KeepAlive has been issued, of course.
 
Upvote 0
Top