user1057197
user1057197

Reputation: 489

android connectivity check

I am currently doing an android application that checks network availability(both wifi & 3G). i have a code that perfectly working in activity . but i need it in broadcast receiver . i want to do some operations when the network is available . i have the permission

plz help me.....

The code is given below...

 ConnectivityManager cm =   (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

 if ( cm.equals(ConnectivityManager.EXTRA_NO_CONNECTIVITY))
    {

  Log.v("CONNECT", "CONNECTED");
    }
 else
 {
  Log.v("CONNECT", "NOT CONNECTED"); 

 }

`

Upvotes: 1

Views: 424

Answers (3)

Parag Chauhan
Parag Chauhan

Reputation: 35936

You should make an BroadcastReceiver that will be triggered when the connectivity status has changed

public class BroadCastSampleActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.registerReceiver(this.mConnReceiver,
                new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
    }
    private BroadcastReceiver mConnReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
            String reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON);
            boolean isFailover = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);

            NetworkInfo currentNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
            NetworkInfo otherNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);

            if(currentNetworkInfo.isConnected()){
                Toast.makeText(getApplicationContext(), "Connected", Toast.LENGTH_LONG).show();
            }else{
                Toast.makeText(getApplicationContext(), "Not Connected", Toast.LENGTH_LONG).show();
            }
        }
    };
}

and then in your AndroidManifest you can check if you have connectivity:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

For more Source code Download

Upvotes: 1

Ashok Domadiya
Ashok Domadiya

Reputation: 1122

private boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager
                .getActiveNetworkInfo();
        return activeNetworkInfo != null;
    }

Upvotes: -1

rekaszeru
rekaszeru

Reputation: 19220

If you have a BroadcastReceiver class declared such:

public class NetConnectionBroadcastReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        final ConnectivityManager cm = (ConnectivityManager) 
            context.getSystemService(Context.CONNECTIVITY_SERVICE);
        final NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        if (networkInfo == null)
            Log.v("CONNECT", "NOT CONNECTED");
        int netType = networkInfo.getType();
        if (netType == ConnectivityManager.TYPE_WIFI)
        {
            Log.v("CONNECT", "CONNECTED TO WIFI");
        }
        else if (netType == ConnectivityManager.TYPE_MOBILE)
        {
            Log.v("CONNECT", "CONNECTED TO MOBILE" + 
                    (networkInfo.isRoaming() ? " ROAMING!" : ""));
        }
    }
}

you can store an instance of it insinde your activity, and in the onCreate method you can register if for the android.net.conn.CONNECTIVITY_CHANGE event:

private final  NetConnectionBroadcastReceiver receiver = 
        new NetConnectionBroadcastReceiver();
public void onCreate(Bundle savedInstanceState)
{
    // ... your onCreate implementation goes here 
    final IntentFilter intentFilter = 
        new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
    this.registerReceiver(this.receiver, intentFilter);
}

When the data connection changes, you get your receiver's onReceive method called. You can examine from within, what kind of change happened.

For this check you don't need more than the android.permission.ACCESS_NETWORK_STATE permission.

Upvotes: 1

Related Questions