Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can register a <a href="http://developer.android.com/reference/android/content/BroadcastReceiver.html"><code>BroadcastReceiver</code></a> to be notified when a WiFi connection is established (or if the connection changed).</p> <p>Register the <code>BroadcastReceiver</code>:</p> <pre><code>IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION); registerReceiver(broadcastReceiver, intentFilter); </code></pre> <p>And then in your <code>BroadcastReceiver</code> do something like this:</p> <pre><code>@Override public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)) { if (intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false)){ //do stuff } else { // wifi connection was lost } } } </code></pre> <p>For more info, see the documentation for <a href="http://developer.android.com/reference/android/content/BroadcastReceiver.html"><code>BroadcastReceiver</code></a> and <a href="http://developer.android.com/reference/android/net/wifi/WifiManager.html"><code>WifiManager</code></a></p> <p>Of course you should check whether the device is already connected to WiFi before this.</p> <p>EDIT: Thanks to ban-geoengineering, here's a method to check whether the device is already connected:</p> <pre><code> private boolean isConnectedViaWifi() { ConnectivityManager connectivityManager = (ConnectivityManager) appObj.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mWifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); return mWifi.isConnected(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload