3

I am creating a sample code, for detecting whether the RJ45 cable is connected or not. I have written a broadcast receiver for the detecting.

@Override
public void onReceive(Context context, Intent intent) {

    String action = intent.getAction();

    if(action.equals(ConnectivityManager.CONNECTIVITY_ACTION)){
        Log.i("Calling Connectivity action in broadcast receiver ", "");
        updateConnectivity(intent);
    }
}

private void updateConnectivity(Intent intent) {

    NetworkInfo info = (NetworkInfo)(intent.getParcelableExtra(
            ConnectivityManager.EXTRA_NETWORK_INFO));

    if (info != null && info.getType() == ConnectivityManager.TYPE_ETHERNET) {
        mEthernetConnected = info.isConnected();
    }

    if(mEthernetConnected)
        Log.i("Ethernet connected status is  ", "" +mEthernetConnected);
    else
        Log.i("Ethernet connected status is  ", "" +mEthernetConnected);}

I am registering this an activity class

intentFilter = new IntentFilter();
    intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);

    mBroadcastReceiver = new EthernetReceiver(this);
    this.registerReceiver(mBroadcastReceiver, intentFilter);

Here in this it is returning only false in logs. can any one has idea how to fix this issue. Thanks in advance.

4

0 回答 0