check internet connection in Sketchware?

To check internet connection in your Sketchware android project, follow the steps given below.

1. In VIEW area of your project, add a LinearLayout linear1. Inside this insert a TextView and write "You are not connected to internet".

2. Add a WebView webview1 below the linear1.

3. In LOGIC area, create a new More Block named Check.

4. Also create a new String variable command and a new Boolean variable connected.

5. In more block Check, first add an add source directly block and add following code:
try {

6. After this set the String command to "ping -c 1 google.com"
7. Next, add another add source directly block and add following code:
connected = (Runtime.getRuntime().exec (command).waitFor() == 0); } catch (Exception e){ showMessage(e.toString());}

Note that the codes in this more block Check, checks if the user is connected to internet or not. If the user is connected to internet, the boolean connected is true, otherwise the boolean connected is false.

8. In onCreate event, first load desired url in WebView and make linear1 GONE. Then use Check block to check if user is connected to internet or not.
9. Next use if..else... block. If user is connected Toast Connected or do any other operation. If user is not connected, make linear1 VISIBLE and webview1 GONE, as shown in image above.

10. Save and run the project.

Watch the video below to understand better:

No comments:

Post a Comment