filipp.kowalski
filipp.kowalski

Reputation: 5605

Wait for internet connection to send data in Android

Is there any possibility to send data when internet will be availabile and app is already off? Let's say I have app which have to send some data, but user doesn't have internet connection on. He turns off the app and later on he turn on wifi. Now I want to send this previous data which was waiting to send. Can I do it somehow?

Upvotes: 3

Views: 3355

Answers (3)

August W. Gruneisen
August W. Gruneisen

Reputation: 227

The recommended way to implement this functionality is with WorkManager

Upvotes: 0

Swayam
Swayam

Reputation: 16354

Yes, it is possible.

Maintain a local database of all your failed requests. Now when the user closes the app, start a Background Service which will be registered to receive a Broadcast message whenever there is a change in connectivity. As soon as connection is established, retrieve the failed requests from the database and fire new requests. The main element in this whole process is the background service which will listen to the broadcast and repeat the requests on the server.

Upvotes: 8

Shobhit Puri
Shobhit Puri

Reputation: 26007

You can monitor for changes in connectivity and receive a broadcast android.net.conn.CONNECTIVITY_CHANGE. When you do receive a broadcast for internet connectivity, check if you were connected to internet. If yes, then check if you've anything pending to send to the server ( You can have some flag saved in Sharedpref or somewhere). Then act accordingly.

Check Broadcast receiver for checking internet connection in android app and http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html

Upvotes: 4

Related Questions