Android Question StartActivity(Main) from Service

GiovanniPolese

Active Member
Licensed 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.
 

lemonisdead

Well-Known Member
Licensed User
Hello

Have you already tried
B4X:
CallSubDelayed(Main,"SubToCall")

If IsPaused(Main) then
  StartActivity(Main)
End if
Even if Erel did not recommand it
 
Last edited:

GiovanniPolese

Active Member
Licensed User
Hi. Thanks. Your suggestion allows one step ahead.. but doesn't resolve. As a matter of fact, while the CallSub(Delayed) issued after the StartActivity seems to have no effect, doing as you suggest .. calls the sub .. when I reactivate manually the main. So the problem remains, in practice. Thanks again.
 

lemonisdead

Well-Known Member
Licensed User
Dear Giovanni,

I don't get the issue you got. Could you please elaborate more ? English is not my primary language. Do you have some troubles while sending values to the sub ? The sub is not executed ? Many thanks

In fact, StartActivity(Main) starts the activity (as I have tested it). Which Android version are you running it ?
 

GiovanniPolese

Active Member
Licensed User
Hi. The problem is simple.
The Main app starts a service, which is a derivation from Sensor example, found in the documentation.
Inside the SensorChanged sub, I do a check on the values.
When a condition is met, I stop the sensor and want to ask something to user.
To do this, I need to load a Layout etc. So I need to call a sub in the Main.
This is rather simple and works, no question, if the Main is active.
Instead, if the Main is paused, the sub in the main is not called.
To verify this, I put a Log message, like Log("Calling sub"), before the CallSub, another Log("Inside sub") inside the called Sub.
In the Log list on the right, first message is shown, second not.
I guessed that solution was simply to StartActivity(Main) if it was paused, as reported in other posts and as you say, but this doesn't happen to me.
Perhaps it could depend on being the CallSub issued inside the SensorChanged sub, which must be a particular one, being that no debug breakpoint can be put inside it.
I am running a 4.1.2 version of Android.
Thanks for your interest.
Giovanni
 

mangojack

Well-Known Member
Licensed User
@DonManfred ... should CallSubDelayed start the paused app/activity immediately(within reason).

I had a similar situation to Giovanni .. the app is paused / closed by user , and all attempts to use CallSubDelayed failed .. Logs Message in Queue for minutes.
 
Last edited:

DonManfred

Expert
Licensed User
the app is paused / closed by user , and all attempts to use CallSubDelayed failed
then it is probably a good idea to use intent-filters and start the intent from the service

but i had tried it first with callsubdelayed and - if i remember correctly - it has worked till now where i use that
 

GiovanniPolese

Active Member
Licensed User
Hi. Here is the example. A service runs a 5 sec timer. At each Tick it calls a sub in the main. Leave your device go to stand-by (Activity pause) and look at the Log. Then reactivate etc. Clearly the Sub in the main is not called when pause, despite the StartActivity ..
 

Attachments

GiovanniPolese

Active Member
Licensed User
Hi. I don't think so: when app becomes paused, the service is still sending the message to Log. Moreover, when I reactivate the app manually, the service will again correctly call the Sub in the Main.. Moreover, it is exactly my purpose to have a service in the background, and an activity that can be paused ..
Anyway, I will try also your suggestions. Thanks.
 

lemonisdead

Well-Known Member
Licensed User
LogCat connected to: LF4TJJIZYL49MJKR
--------- beginning of /dev/log/system
--------- beginning of /dev/log/main
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (sr1) Create **
** Service (sr1) Start **
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
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
Before call
sending message to waiting queue (CallSubDelayed - SubToCall)
** Activity (main) Pause, UserClosed = false **
Before call
sending message to waiting queue (CallSubDelayed - SubToCall)
Before call
sending message to waiting queue (CallSubDelayed - SubToCall)
Before call
sending message to waiting queue (CallSubDelayed - SubToCall)
running waiting messages (4)
Sub Called ----------- OK
Sub Called ----------- OK
Sub Called ----------- OK
Sub Called ----------- OK
** Activity (main) Resume **
Before call
Sub Called ----------- OK
** Activity (main) Pause, UserClosed = false **
** Activity (main) Create, isFirst = false **
** Activity (main) Resume **
** Service (sr1) Start **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Create, isFirst = false **
** Activity (main) Resume **
** Service (sr1) Start **
** Activity (main) Pause, UserClosed = false **
Before call
sending message to waiting queue (CallSubDelayed - SubToCall)
running waiting messages (1)
Sub Called ----------- OK
** Activity (main) Resume **
Before call
Sub Called ----------- OK
Before call
Sub Called ----------- OK
** Activity (main) Pause, UserClosed = false **
Before call
sending message to waiting queue (CallSubDelayed - SubToCall)
In the first part of the logs you'll see the action using CallSubDelayed instead of CallSub
You can see 4 "OK" when I have reactivated the Activity

After that, I had modified your code and put StartActivity after CallSubDelayed. You can see the "OK" is printed each time


B4X:
Sub Tick_Tick


  Log("Before call")
  CallSubDelayed(Main,"SubToCall")
  If IsPaused(Main) Then StartActivity(Main)
End Sub
 
Last edited:

GiovanniPolese

Active Member
Licensed User
I don't understand. This is my Tick_Tick:
Sub Tick_Tick
Log("Before call")
CallSubDelayed(Main,"SubToCall")
IfIsPaused(Main) ThenStartActivity(Main)

End Sub

And this is my log:

Resuming
** Service (sr1) Create **
** Service (sr1) Start **
Before call
Sub Called ----------- OK
Before call
Sub Called ----------- OK
Before call
Sub Called ----------- OK
Before call
Sub Called ----------- OK
Before call
Sub Called ----------- OK
Before call
Sub Called ----------- OK
Before call
Sub Called ----------- OK
Before call
Sub Called ----------- OK
Before call
Sub Called ----------- OK
Before call
Sub Called ----------- OK
Before call
Sub Called ----------- OK
** Activity (main) Pause, UserClosed = false **
Before call
sending message to waiting queue (CallSubDelayed - SubToCall)
Before call
sending message to waiting queue (CallSubDelayed - SubToCall)
Before call
sending message to waiting queue (CallSubDelayed - SubToCall)
Before call
sending message to waiting queue (CallSubDelayed - SubToCall)
Before call
sending message to waiting queue (CallSubDelayed - SubToCall)

As You see, after pausing the SubCalled message is not issued. Of course I didn't reactivate the Main manually, because this is not what I want ..
 

lemonisdead

Well-Known Member
Licensed User
Sorry, I was editing my previous message to provide the information
In the first part of the logs you'll see the action using CallSubDelayed instead of CallSub
You can see 4 "OK" when I have reactivated the Activity

After that, I had modified your code and put StartActivity after CallSubDelayed. You can see the "OK" is printed each time
If you don't reactivate the Activity, it is still in Pause and I think the waiting messages are still not executed
 

GiovanniPolese

Active Member
Licensed User
Thanks again for your trials, but I already put StartActivity before or after, CallSub delayed or not. No change. I will check that link that you gave me now.
 
Top