Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Just create a <code>broadcast receiver</code> with <code>CONNECTIVITY_CHANGE</code> action. And you will get a <code>broadcast</code> whenever network connectivity will change.</p> <p><strong>NetworkUtil.java</strong></p> <pre><code>public class NetworkUtil { public static int TYPE_WIFI = 1; public static int TYPE_MOBILE = 2; public static int TYPE_NOT_CONNECTED = 0; public static int getConnectivityStatus(Context context) { ConnectivityManager cm = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); if (null != activeNetwork) { if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) return TYPE_WIFI; if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) return TYPE_MOBILE; } return TYPE_NOT_CONNECTED; } public static String getConnectivityStatusString(Context context) { int conn = NetworkUtil.getConnectivityStatus(context); String status = null; if (conn == NetworkUtil.TYPE_WIFI) { status = "Wifi enabled"; } else if (conn == NetworkUtil.TYPE_MOBILE) { status = "Mobile data enabled"; } else if (conn == NetworkUtil.TYPE_NOT_CONNECTED) { status = "Not connected to Internet"; } return status; } } </code></pre> <p><strong>Broadcast Receiver to handle changes in Network state</strong></p> <pre><code>public class NetworkChangeReceiver extends BroadcastReceiver { @Override public void onReceive(final Context context, final Intent intent) { String status = NetworkUtil.getConnectivityStatusString(context); Toast.makeText(context, status, Toast.LENGTH_LONG).show(); } } </code></pre> <p><strong>AndroidMenifest.xml</strong></p> <pre><code>&lt;application ...&gt; ... &lt;receiver android:name="net.viralpatel.network.NetworkChangeReceiver" android:label="NetworkChangeReceiver" &gt; &lt;intent-filter&gt; &lt;action android:name="android.net.conn.CONNECTIVITY_CHANGE" /&gt; &lt;action android:name="android.net.wifi.WIFI_STATE_CHANGED" /&gt; &lt;/intent-filter&gt; &lt;/receiver&gt; ... &lt;/application&gt; </code></pre> <p><strong>UPDATE</strong></p> <p><strong>Permissions required to access network state:</strong></p> <pre><code>&lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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