Android Question Start service depending on connectivity

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Hello all,

Is there any way to automatically start a service when the internet connectivity is back after a period down?
 

DonManfred

Expert
Licensed User
Longtime User
Last edited:
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
No.
You need to have a running service to get this information at all.
There is no event for this in Android.

Edit:
https://developer.android.com/training/monitoring-device-state/connectivity-monitoring#java

Probably possible using a Broadcastreceiver. But the receiver must be started in a service and the process must still be active if i read it right.
So i guess it will not work if your app is killed/not started
Hello Don,

thanks for answering (again :)). This resource could be very interesting for the situation when an app uses the internet to receive or send instant messages or other real time data that could be queued in background in moments without internet. Instead of maintaining a service running, if we could program a service that wakes up whenever the connection is reestablished after a down period, we could save battery and increase a lot the app efficiency doing the dispatch near instantly when reconnected. I think that should have any way to start a service when connectivity is up, similar to push messages but with a "local flag to android"...
I'll study the article to find is there is a way and post here if find anything.

Thanks!
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Just use a service to test for internet connectivity every so often, when there's internet connectivity just do whatever you need your app to do...
Hello Peter... Ok, I can schedule a service each, let's say, 30 seconds... but imagine:
- Running each 30 seconds will clearly spend more battery than a service called by push, for example.
- In an unstable connection, the time when service starts could not be the same time frame when the internet is available (this is very significant, for example, when I have to send a small message with a few bytes and only 5 or 10 seconds of connectivity could be enough).
- And finally, in messenger services like those that we manage here it's very common to have two users testing the messages efficiency putting two mobile devices side by side and checking how much time a remote system will spend to receive a message after internet connection... 30 seconds is A LOT in this cases. Of course that the use of Firebase Cloud Messages increases the response efficiency for incoming messages due the push function starting the services, but for queued outgoing messages I cant see other way instead of maintaining a foreground service while there is queue... and this is not good because I have reports from many users about the "dislike" of persistent apps notifications...
I'll study a little bit more and post here if I find new solutions.
 
Upvote 0
Top