reader1
reader1

Reputation: 93

On Connection to Internet?

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?

Upvotes: 1

Views: 432

Answers (1)

Samuh
Samuh

Reputation: 36484

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.

Upvotes: 3

Related Questions