Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait for internet connection to send data in Android

Tags:

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?

like image 295
filipp.kowalski Avatar asked Dec 01 '25 21:12

filipp.kowalski


2 Answers

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.

like image 103
Swayam Avatar answered Dec 04 '25 12:12

Swayam


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

like image 20
Shobhit Puri Avatar answered Dec 04 '25 13:12

Shobhit Puri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!