Android Question startserviceatexact not workin as expected

jayel

Active Member
Licensed User
Hello,

my boss want to know if there is internet connection with my app, so he is sure that al the notifications will be send to the phone.

I implemented a service called "checkinternet", and run it for test purposes every 30 seconds. Normally this will be 10 minutes.

I wrote a little test app, but when my phone gets in sleep mode the check doesn't work anymore.
Can somebody help please?

See zip in attachment
 

Attachments

  • testconnection.zip
    9.9 KB · Views: 104

jayel

Active Member
Licensed User
OK make sense, but how would you test this?
How can I know that the app is doing it right?
I want to be sure that the checkinternet is doing its work even when the phone is in sleep mode.
 
Upvote 0

ronell

Well-Known Member
Licensed User
maybe checking if main is pause then update something else?
B4X:
Sub Service_Start (StartingIntent As Intent)
    Log("Checkinternet Service started")
    Wait For(CheckinternetReal) Complete (Result As Boolean)
    StartServiceAtExact("",DateTime.Now + 30*1000,True)
    Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
    
    If IsPaused(Main) Then
        .... 
    Else
        CallSubDelayed2(Main, "UpdateLabel","Resultaat van download : " & Result)
    End If
    

End Sub
 
Upvote 0

jayel

Active Member
Licensed User
hmm i see this in the log (with #bridgelogger: true)
upload_2018-8-16_11-1-12.png

So the service is destroyed???
It has to continue....
 
Upvote 0

jayel

Active Member
Licensed User
Now i added a sql log, so when phone is in sleep mode it will log into database.
But nothing happeness, I get 3 lines in my table, but after a while the service doesn't do the job.

I am not understanding the concept, I just want to check if there is internet, even when the phone is sleeping...

See attachment
 

Attachments

  • testconnection.zip
    10.2 KB · Views: 95
Upvote 0

jayel

Active Member
Licensed User
can somebody confirm that the above code (in zip attachment) is working on there phone? even if it is in sleep mode?
 
Upvote 0

KMatle

Expert
Licensed User
So what does your boss do when the device has no internet connection and you show a message or alarm him :D

What I do

- send an FCM message from my server
- if the device has received the message, use a httpjob to commit the reception
- if there's no commit for x time, send messages to defined devices (e.g. yours) to be alarmed that your boss is "out of reach"

Optionally:

- move your bosses device (and your boss perhaps) back to an area where there's reception
 
Upvote 0

jayel

Active Member
Licensed User
So what does your boss do when the device has no internet connection and you show a message or alarm him :D

What I do

- send an FCM message from my server
- if the device has received the message, use a httpjob to commit the reception
- if there's no commit for x time, send messages to defined devices (e.g. yours) to be alarmed that your boss is "out of reach"

Optionally:

- move your bosses device (and your boss perhaps) back to an area where there's reception

Yes, I was also thinking of sending a FCM message every 10 minutes, my app could start a timer and set its interval to 12 min, when it receives a message, when the tick event occurs then an alarm on the phone would notify my boss that there is no connection to internet.
Should i set and reset my timer in my starter service? becaus I want to be sure that the timer will work even when my phone is in sleep mode.
 
Upvote 0

Anser

Well-Known Member
Licensed User
The minimum period for StartServiceAt is 30 minutes. Android will not allow you to start your app more frequently. You can keep a foreground service and let your app run all the time.
This is a new information that the minimum period for StartServiceAt is 30 minutes. I was testing my app with 15 minutes and was not working continuously.
 
Upvote 0

jayel

Active Member
Licensed User
hmmm in attachment a third try... and I get following error :
Checkinternet timer started
sending message to waiting queue (CallSubDelayed - UpdateLabel)
Checkinternet timer started
sending message to waiting queue (CallSubDelayed - UpdateLabel)
running waiting messages (3)
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
Checkinternet timer started
sending message to waiting queue (CallSubDelayed - UpdateLabel)
Checkinternet timer started
sending message to waiting queue (CallSubDelayed - UpdateLabel)
** Service (starter) Destroy (ignored)**
** Service (httputils2service) Destroy **
Checkinternet timer started
java.lang.RuntimeException: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=be.robotronic.testconnection/anywheresoftware.b4a.samples.httputils2.httputils2service }: app is in background uid UidRecord{488786e u0a349 CAC bg:+1m15s73ms idle procs:1 seq(0,0,0)}
at anywheresoftware.b4a.keywords.Common$11.run(Common.java:1157)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=be.robotronic.testconnection/anywheresoftware.b4a.samples.httputils2.httputils2service }: app is in background uid UidRecord{488786e u0a349 CAC bg:+1m15s73ms idle procs:1 seq(0,0,0)}
at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1538)
at android.app.ContextImpl.startService(ContextImpl.java:1484)
at android.content.ContextWrapper.startService(ContextWrapper.java:663)
at anywheresoftware.b4a.keywords.Common.StartService(Common.java:884)
at anywheresoftware.b4a.keywords.Common$11.run(Common.java:1127)
... 7 more
*** Service (starter) Create ***
** Service (starter) Start **
Starter Service started
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
Checkinternet timer started
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
** Activity (main) Pause, UserClosed = false **
 

Attachments

  • testconnection.zip
    10.3 KB · Views: 107
Upvote 0

jayel

Active Member
Licensed User
Yes I now, my first app a uploaded was with startserviceat but it didn't work.
I read that the starter service continues to work, even when in sleep mode, so I tried it like last upload file.
But now it seems that a timer in the starter service also don't continue.

Can somebody please explain why? A simple timer that put a record in a sqlite database, from a starter service doesn't do the work.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
The starter service doesn't continue to work when the OS kills the process.

The improvements in v8.30 is related to a change in Android 8. Starting from Android 8 the OS can kill services of your app without killing the process. The starter service will not be killed by the OS. It will only be killed when the complete process is killed.
 
Upvote 0
Top