android how to get indicate when the internet connection is lost?

后端 未结 4 1044
忘掉有多难
忘掉有多难 2020-12-09 04:44

I\'m developing an android application and I want to get a notification when the internet (wifi or packet data connection) connection is lost. On my approach I can get the s

相关标签:
4条回答
  • 2020-12-09 04:50

    You need to set up a broadcast receiver.

    Add these as intent filters.

                <intent-filter>
                    <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                    <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
                </intent-filter>
    
    0 讨论(0)
  • 2020-12-09 05:01

    For WIFI you could register a broadcast receiver as:

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
    registerReceiver(broadcastReceiver, intentFilter);
    

    You can also register the receiver in the Manifest.

    Then in your receiver:

    @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
            }
        }
    }
    

    For any type of data connection listeners you could use the following receiver registered as:

    <receiver android:name=".receiver.ConnectivityReceiver">
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
        </intent-filter>
    </receiver>
    

    and the in your ConnectivityReceiver:

    public class ConnectivityReceiver extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
        }
    }
    

    In the onReceive method you could check if you have internet connectivity or not using this developer article.

    0 讨论(0)
  • 2020-12-09 05:06

    You should use BroadcastReceivers for your requirement. Create a BroadcastReceiver which listen for network changes.

    Incase of any change in the network it will automatically fire the event and you can perform your operations regarding it.

    0 讨论(0)
  • 2020-12-09 05:14

    use following code:

    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import classes.NetworkUtil;
    
    public class NetworkChangeReceiver extends BroadcastReceiver {
    
            @Override
            public void onReceive(final Context context, final Intent intent) {
    
                boolean IsConnected = NetworkUtil.getConnectivityStatusString(context);
               // Toast in here, you can retrieve other value like String from NetworkUtil
               // but you need some change in NetworkUtil Class
            }
        }
    

    and NetworkUtil is:

    package classes;
    
    import android.content.Context;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    
    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 boolean getConnectivityStatusString(Context context) {
            int conn = NetworkUtil.getConnectivityStatus(context);
            boolean status = false ;
            if (conn == NetworkUtil.TYPE_WIFI) {
                status = true;
            } else if (conn == NetworkUtil.TYPE_MOBILE) {
                status = true; 
            } else if (conn == NetworkUtil.TYPE_NOT_CONNECTED) {
                status = false;
            }
            return status;
        }
    }
    

    and in manifest file:

     <receiver
                android:name="receiver.NetworkChangeReceiver" >
                <intent-filter>
                    <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                </intent-filter>
            </receiver>
    

    and this permission:

     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    
    0 讨论(0)
提交回复
热议问题