I have an Activity which looks up data from the web in its onCreate method. The Activity is activated by the user hitting a notification. So it is a common problem that the user will quickly turn on their phone, unlock it, slide open notifications, tap the notification, and the Activity will activate before the phone is done connecting to internet.
I do have a friendly AlertDialog that pops up informing the user that the data couldn’t be received and to try again when the network is connected; but is there a way for the Activity to actively tell the phone to connect and detect that a connection is being made and then wait for the connection to establish, and then load its data successfully?
Usually, you would do something like this:
The idea is ask the user to go and configure a network connection. Then, if the user does want to configure it, you will call the
Settings.ACTION_WIRELESS_SETTINGSintent.Also, notice the
isOnlinevariable, which is a boolean that tells whether there’s a network connection or not. In order to set that variable you can use an external simple class like this:Also, you will have to add this permission to your
AndroidManifest.xmlfile: