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.

user1948220
  • 73
  • 1
  • 1
  • 7
  • Do you have enabled this permission in your AndroidManifest.xml ? `` – Halim Qarroum Jan 04 '13 at 11:13
  • @HQarroum Thanks for giving me a reply. i had already given the permission in android manifest file. – user1948220 Jan 04 '13 at 11:16
  • The Log class in Android takes for its static methods as a first parameter the tag you want to identifiy your log with and the message as a second argument. In your example, the first dumped log is `Log.i("Calling Connectivity action in broadcast receiver ", "");` where you're giving `""` as a message. Try using something like Log.i("MyRJ45Broadcast", "Connectivity has changed"); and see if you get this message in the logcat. – Halim Qarroum Jan 04 '13 at 11:21

0 Answers0