How can I monitor the network connection status in

2019-01-02 15:09发布

问题:

I have a Service that is downloading a file from a server. I warn the user to only download the file over Wireless LAN before the download starts.

My problem is that my download gets stuck if I loose the network connection. Is there a way to listen for changes in network connection or if it is lost entirely?

回答1:

Listen for CONNECTIVITY_ACTION

This looks like good sample code. Here is a snippet:

BroadcastReceiver networkStateReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.w("Network Listener", "Network Type Changed");
    }
};

IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);        
registerReceiver(networkStateReceiver, filter);


标签: