MIT App Inventor code snippet to verify an Internet connection with the use of Android’s webviewer function
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:
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.
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).
Feel free to contact me for any questions or comments. It is always a pleasure to help out!