Android Question Android8 - App is paused (in background), how to wake up this app from service?

petr4ppc

Well-Known Member
Licensed User
Longtime User
Dear friends,

how can I wake up the app which is Paused (in background)?
I have used before Andr.8 in service module this:
B4X:
StartActivity(Main)
everything was OK.

How can resume this App, please in Android 8?
Best regards
p4ppc

I have read forum, for example this:
https://www.b4x.com/android/forum/threads/recover-title-and-message-from-firebase.94771/

but it looks that it is impossible to resume the App from service programaticaly (in Adndroid 8?)
 
Last edited:

Semen Matusovskiy

Well-Known Member
Licensed User
Did you try moveTaskToFront ?

I attached simple example, which works in my emulator (API 26).

The app runs and disappears per 15 seconds. In this time run another app (for example, webbrowser).
 

Attachments

  • test.zip
    6.1 KB · Views: 294
Last edited:
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
did you try setting the service in foreground modus?
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
but it is not functioned. Your upp is not run after I put the App to the background.

Take a look another sample (in https://www.b4x.com/android/forum/t...e-up-this-app-from-service.96792/#post-610561 )

Added later.

Just now I make a library to exclude inline java statements in my app.
In updated test.zip you can find library (jar, xml) and Basic test application.

During experiments I found that StartActivity (Main) in my sample does not raise Activity_Create event. Activity_Resume only.

This means that StartActivity (Main) was enough from the beginning (I tested in API 26 & 27 emulator).

After experiments with Starter service I decided to check ordinary service. Works fine also.
Can you make a sample, where StartActivity (Main) is not enough ?
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
During experiments I found that StartActivity (Main) in my sample does not raise Activity_Create event. Activity_Resume only.
There is no need to experiment. The activities life cycle is well defined.

Are you sure that the service is still running? Are you using StartServiceAt to start the service?
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
Erel - The activities life cycle was not a target of experiments :)

BTW, there is interesting article https://developer.android.com/about/versions/oreo/android-8.0-changes , which explains changes for services.

I reconstructed a sample and tested under API 26/27
Run sample and switch to another app. After 10 seconds your app appears again.
Works fine without any "clever" functions.

I compiled in B4A 8.30 (this is important). Do you use this release ?
 

Attachments

  • test.zip
    1.4 KB · Views: 273
Last edited:
Upvote 0

petr4ppc

Well-Known Member
Licensed User
Longtime User
Dear friends,

EREL- yes I am using in Sub Service_Start, this:
B4X:
StartServiceAt("", DateTime.Now + .25 * 60 * 1000, True)
But I think, that sometimes is this service killed when I put App to PAUSE mode and
B4X:
If IsPaused(Main)=True Then StartActivity (Main)
is not functioned.

SEMEN - thank you for your help, I am trying your each uploaded project, but I think, that sometimes it functioned perfect, sometimes it is no functioned.
The last project is without your library. Its true? This last example is very easy and I think that useful in simply Apps. I dont know if I have true.

I am still trying and finding best solution,
thank you
p4ppc
 
Upvote 0

petr4ppc

Well-Known Member
Licensed User
Longtime User
SEMEN, yes I have 8.30...I am testing it on the phone and tablet with A8.
As I wrote - it is possible, that I am doing something wrong, but I am trying to do everything right....Maybe I need to do more tests
But - thank you very much for your help, you give me the direction - how to do tests. Situation with A8 is really different than with earlier version :eek:
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
I made experiments on my enough fast smartphone OnePlus 5T (A5010).
In general behaviour was the same as in emulators until I began to start many applications. Happens the same what you wrote - sometimes my example does not restore window. I looked Log. Service starts activity, but there is no Activity_Resume event. More ... I see attempt to close Starter service
** Service (starter) Destroy (ignored)**
After this it looks that Android forgets about my app (until I activate it again in recent list).

Well, at least I can reproduce a problem.
I will look ... If OS is able to activate app again, hope, we will be able to do this also.
 
Last edited:
Upvote 0

petr4ppc

Well-Known Member
Licensed User
Longtime User
SEMEN - as you wrote :
After this it looks that Android forgets about my app (until I activate it again in recent list).

I see the same. After I push round button on my phone first time, the app is refreshed, second time - the app is refreshed, third time - app is staying in PAUSE mode.
If I push the rectangle button on my phone, app is refreshed each time. I put here picture - to attachement - only for imagination, what is round button and rectangle button. So the trouble is that app is staying in PAUSE mode.

thank you
p4ppc
 

Attachments

  • vvv.jpg
    vvv.jpg
    23.8 KB · Views: 312
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
Services are not a serious problem. As I understand foreground services work in Oreo exactly like in Android 7-.
I made simple test. Activity Main:
B4X:
Sub Process_Globals
    Public booleanIsRunning As Boolean
End Sub

Sub Globals
End Sub

Sub Activity_Create (FirstTime As Boolean)   
    Activity.Color = Colors.RGB (Rnd (0, 255), Rnd (0, 255), Rnd (0, 255))
End Sub

Sub Activity_Resume   
    booleanIsRunning = True
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    booleanIsRunning = False
    StartService (Secondary)   
End Sub

and service Secondary:

B4X:
Sub Process_Globals
    Private objectTimer As Timer
End Sub

Sub Service_Create
    Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_ALWAYS  ' Theoretically, not needed
End Sub

Sub Service_Start (StartingIntent As Intent)
    objectTimer.Initialize ("timer", 10000)
    objectTimer.Enabled = True
    Log ("Wait 10 seconds")
End Sub

Sub timer_Tick   
    objectTimer.Enabled = False
    Log ("StartActivity")
    StartActivity (Main)   
    Do While (Main.booleanIsRunning = False)
        Sleep (100) 
    Loop
    StopService (Me)   
End Sub

After some time the service (which is foreground) starts Activity, but Android does not activate an activity. Why ?
 
Upvote 0

petr4ppc

Well-Known Member
Licensed User
Longtime User
SEMEN - this is what I described earlier. I have the same experience and questions and I have not answer. I am doing still tests.

Did you tryed start your service as wrote Erel?
B4X:
StartServiceAt("", DateTime.Now + .25 * 60 * 1000, True)
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
Not so easy to check

New test :
1) Activity Main
B4X:
Sub Process_Globals
End Sub

Sub Activity_Create (FirstTime As Boolean)
    Activity.Color = Colors.RGB (Rnd (0, 255), Rnd (0, 255), Rnd (0, 255))
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    StartServiceAt (Secondary, DateTime.Now + .25 * 60 * 1000, True)
End Sub

Service Secondary
B4X:
Sub Process_Globals
End Sub

Sub Service_Start (StartingIntent As Intent)
    StartActivity (Main)
    StopService (Me)  
End Sub

In both cases it's neccessary to do a lot of switches until the program will die.
It looks that Android calculates summary time in background and compares with the limit.
When limit is over, Android holds the app per some time and gives new limit.
So, sooner or later an incindent will occur.

My summary - to minimize time of background processes as possible.

In Activity_Pause (before StartServiceAt) stop other services by yourself.
If not possible, make services foreground (Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_ALWAYS)
 
Last edited:
Upvote 0

petr4ppc

Well-Known Member
Licensed User
Longtime User
SEMEN - I am at the end. With your example I have this results: if I push round button in Android device then first or senod or third time is App refreshed and then nothing. As you describe in your messages. I dont know what can I do...

Please have somebody some example - how to refresh app from service correctly when the app is in PAUSE mode?
 
Last edited:
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
@petr4ppc

On my phone the problem with my sample occurs after a lot of restarts.
My latest test.zip based on Erel's advice about StartServiceAt and foreground service.

Anyway, it's not a solution, because there is no garantee that activity will be started when we want.

It's not absolutelly clear how Android calculates time in background and holds processes. But it's possible to expect that Android binds background time to PID (Process ID). Probably, time to foolish Android and to restart itself with new PID, using pending intent.
Approximatelly so (the code from stackoverflow, easy to transform
B4X:
Intent mStartActivity = new Intent(context, StartActivity.class);
int mPendingIntentId = 123456;
PendingIntent mPendingIntent = PendingIntent.getActivity(context, mPendingIntentId,    mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager mgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
System.exit(0);

Not very comfortable (we need to stop services etc). What do you think ?
 
Upvote 0

petr4ppc

Well-Known Member
Licensed User
Longtime User
I think, that I must be grateful to you, because you are spending with me lot of time :) thank you very much.
I will try your theme. I think before Android 8 it was everything more clearer. I go to try more experiments
 
Upvote 0
Top