适用于RJ45以太网线的Android广播接收器

时间:2013-01-04 11:11:01

标签: android broadcastreceiver ethernet connectivity

我正在创建一个示例代码,用于检测RJ45电缆是否已连接。我已经为检测编写了一个广播接收器。

@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);}

我正在注册这个活动类

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

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

在此处,它仅在日志中返回false。任何人都知道如何解决这个问题。提前谢谢。

0 个答案:

没有答案