The Quick, Dirty, and 99% Reliable Method
Better yet, simply use AutoTools' Connectivity event or action. While I generally don't recommend plug-ins, unless absolutely necessary, AutoTools is definitely an essential.
The Right Way™
If you want to do it, "the right way," although considerably more convoluted, use the new Get Network Info
action and check the capabilities, like so:
Task: Disconnect Wi-Fi if No Internet
<Pause and allow for network conditions and checks to settle>
A1: Wait [
MS: 0
Seconds: 3
Minutes: 0
Hours: 0
Days: 0
Continue Task After Error:On ]
A2: Get Network Info [
Continue Task After Error:On ]
<Wi-Fi Connected to AP<br />
<b>AND</b><br />
(Does NOT have internet<br />
<b>OR</b><br />
Has NOT been validated<br />
<b>OR</b><br />
Captive Portal PRESENT)>
A3: If [ %ni_active_types(#?wifi) > 0 & %ni_active_capabilities(#?internet) < 1 | %ni_active_capabilities(#?validated) < 1 | %ni_active_capabilities(#?captive_portal) > 0 ]
A4: WiFi [
Set: Off
Continue Task After Error:On ]
A5: End If
(Scroll to the right at the bottom, in the If
conditions of A3
, of the above code block to see where the magic really happens.)
The above allows for a variety of network scenarios to occur without prematurely judging the state of internet connectivity; namely, Captive Portal logins. It also prevents you from having to manually ping Google.com (which has its own pitfalls).
... One Step Further
You could make this much more robust too by associating the above task with a profile that handles things on an event-driven basis when network conditions change, like so:
Profile: Wi-Fi: Network Change Detected
Settings: Priority: 20 Enforce: no
Event: Intent Received [ Action:android.net.conn.CONNECTIVITY_CHANGE Cat:None Cat:None Scheme:* Mime Type:* ]
Enter Task: Anon
Settings: Abort Existing Task
Task: Disconnect Wi-Fi if No Internet
<Pause and allow for network conditions and checks to settle>
A1: Wait [
MS: 0
Seconds: 3
Minutes: 0
Hours: 0
Days: 0
Continue Task After Error:On ]
A2: Get Network Info [
Continue Task After Error:On ]
<Wi-Fi Connected to AP<br />
<b>AND</b><br />
(Does NOT have internet<br />
<b>OR</b><br />
Has NOT been validated<br />
<b>OR</b><br />
Captive Portal PRESENT)>
A3: If [ %ni_active_types(#?wifi) > 0 & %ni_active_capabilities(#?internet) < 1 | %ni_active_capabilities(#?validated) < 1 | %ni_active_capabilities(#?captive_portal) > 0 ]
A4: WiFi [
Set: Off
Continue Task After Error:On ]
A5: End If
(Take special note of the Task Setting of, Abort Existing Task
since this intent gets fired about 6-12 times whenever there is a network state change and you don't want your Task to be executing at the very beginning of those intents, otherwise network states won't be correct yet.)
Also, make sure you're not in beginner mode or in Tasky, and ensure all steps have, Continue on Error
checked.
That is the proper way to detect internet connectivity: by allowing Android to do the heavy-lifting so that ALL scenarios are accounted for.