MIT App Inventor code snippet to verify an Internet connection with the use of Android’s webviewer function

Updated on / dernière mise à jour : 11/25/2018

Many Android App creators use the webviewer function included in MIT’s App Inventor to let their app load an HTML website, which can also be pretty useful for online data requiring the use of the Internet and/or the inclusion of ads from Adsense, Chitika or Bidvertiser, notably. This being said, many apps connect directly to websites without checking if an Internet or WiFi connection is available. When this happens, app users only see a page informing them that the page could not be loaded, which is also a good reason for them to uninstall your app quickly from their Android phone.

Hence, it is a good idea to include to your App Inventor .apk file a code snippet that will check before launching the website if an Internet or WiFi connection is available. If so, it will load the page with the webviewer function and if such a connection is not available, it will simply inform the user that the app requires a connection without launching the website. Let’s see this as an intelligent splash screen :).

Here’s the code:

 

Check Internet connection prior to launching Android's webviewer function

Download file: ConnectionCheck.zip

A few things to consider:

1- googleUrl is set to http://www.google.com since Google is always available as a website. The app will thus verify if the phone is connected to the Internet of WiFi by trying to connect to Google. The response will either be 200 (connection available) or 1101 (connection unavailable). If it is available, WebViewer1.GoToUrl will trigger the loading of your website. If it is not available, the message “No connection”, “Connection error” or “Program error” will appear followed by “Internet connection is required for this app”. You can change the desired message under Label1.Text.

Screen1

2- It is also important to use a minimum of two screens for the app, one for the splash screen and the other one for the website to appear on through the webviewer function. Screen 1 must be visible while screen 2 must be hidden. The width and height of both screens must be set to “Fill parent…” (very important).

ScreenArrangement1

ScreenArrangement2

Feel free to contact me for any questions or comments. It is always a pleasure to help out!

Best regards.

Robert Radford

Robert Radford

[email protected]

guest
16 Comments / Commentaires
Newest / Plus récent
Oldest / Plus ancien Most Voted
Inline Feedbacks
View all comments / Voir tous les commentaires
Al

Hi,
I desperately need to open Wifi seting page in my app but i couldn’t find any clue

Al

Thanks a lot, I have solved already.

Grant Bresler

Hi, I currently have success with this code having created an .apk app inventor 2, however it does not run on a Samsung S5 running KitKat? It does work on a Note3, upgraded to KitKat 4.4.2

Grant Bresler

Thanks Robert, I have tried this and still no luck.

onur

.aia pls download link

Sklep

Very energetic post, I loved that a lot. Will there be a part 2?

Bernd Montes

Very good blog! Do you have any helpful hints for aspiring writers?
I’m hoping to start my own blog soon but I’m a little lost on everything.
Would you recommend starting with a free platform like WordPress or go for a paid option?
There are so many options out there that I’m totally confused ..
Any tips? Many thanks!

Relogio D.

I am actually thankful to the owner of this website who has shared this fantastic
article at at this place.

Mehul

I am new to MIT App Inventor and trying to create a project but i am not able to write the above code so can you plz send me your aia file

%d bloggers like this: