7

Possible Duplicate:
How can I monitor the network connection status in Android?

I need to continuously check whether the internet is connected or not and update the text area accordingly with appropriate message . Now if i create an asynctask it will execute once and stop which is not what I want . I want to check at every moment continuously and obviously this should not be on the main thread .So Service wont be a good choice either . Can anyone help me What is the best and efficient approach to handle this . Thanks

Community
  • 1
  • 1
Vyper
  • 573
  • 1
  • 6
  • 17

2 Answers2

9

do it with a receiver. you can be notified about network state change. for example,

private BroadcastReceiver networkReceiver = new BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
     super.onReceive(context, intent);
     if(intent.getExtras()!=null) {
        NetworkInfo ni=(NetworkInfo) intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO);
        if(ni!=null && ni.getState()==NetworkInfo.State.CONNECTED) {
            // we're connected
        }
     }
     // we're not connected 
   }
}

register this in your onResume(), and unregister on onPause().

@Override
protected void onPause() {
    super.onPause();
    unregisterReceiver(networkReceiver);
}

@Override
protected void onResume() {
    super.onResume();
    IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
    registerReceiver(networkReceiver, filter);
}

additionally, to obtain the initial state before your receiver has been registered, call this in your onResume() method,

public boolean isNetworkConnected() {
        ConnectivityManager cm =
            (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()) {
            return true;
        }
        return false;
    }

make sure your app requests this permission,

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Jeffrey Blattman
  • 22,176
  • 9
  • 79
  • 134
  • When I am switching my wifi on and off The broadcast receiver is not working . The Onresume code is working perfect but the broadcast receiver is not notifying me any change . any help ? – Vyper Sep 25 '12 at 18:47
  • I just figured out the mistake thanks! – Vyper Sep 25 '12 at 19:08
  • @jeffrey hey i check the internet connection using your code.but giving error in orResume() method.. registerReceiver(networkReceiver); in this method registerReceiver ask for 2 parameter. 1 for broadcast receiver object and @ for intent filter. wht i pass insted of 2? – Google Mar 08 '13 at 06:31
  • Great answer! I did not require `isNetworkConnected()` method though because `registerReceiver` always trigger `onReceive()` the **first time** it is registered. – daisura99 Sep 08 '16 at 04:19
2

You don't need to constantly ask the OS if there is a network connection. Simply check whether a connection exists in onResume() and then use a BroadcastReceiver to listen for the CONNECTIVITY_ACTION Intent from the ConnectivityManager when the connection is changed and check EXTRA_NO_CONNECTIVITY.

Sam
  • 86,580
  • 20
  • 181
  • 179