1

Is there some way of executing a block of code as soon as the device connects to the internet?

The situation is this: Currently I have an application that sends information to a server. This works fine when the device is connected. However, when there is no connection, the information never gets sent. Is there some way of putting this code on hold until the user connects to the internet? Kinda like an 'onInternetConnectionEstablished()'. Or perhaps a way of having a service block or loop until a connection is established?

reader1
  • 93
  • 1
  • 8

1 Answers1

3

Write a BroadcastReceiver that listens to android.net.ConnectivityManager.CONNECTIVITY_ACTION. See the documentation of ConnectivityManager class.

You will need android.permission.ACCESS_NETWORK_STATE in your manifest.

This thread might interest you.

Community
  • 1
  • 1
Samuh
  • 36,316
  • 26
  • 109
  • 116