2 roblonely2 roblonely2 于 2014.12.12 11:19 提问

android中如何获取获取以太网连接状态

有什么方法可以获取当前以太网是否连接的状态,已知现在EthernetManager类无法在应用层直接调用,
那么有没有其他方法可以获取以太网是否连接的状态

4个回答

jiangbing0125
jiangbing0125   2014.12.12 12:37

public class WifiStateReceiver extends BroadcastReceiver {
private WifiStateListener listener;

public WifiStateReceiver(WifiStateListener listener){
    this.listener = listener;
}

public interface WifiStateListener {
    void onWifiStateChange(boolean connected);
}

@Override
public void onReceive(Context context, Intent intent) {
    // TODO 启动一个线程合适吗????要不要换成ConnectionChangeReceiver
    if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
        int wifistate = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_DISABLED);
        D.loge("wifistate:"+wifistate);
        if (wifistate == WifiManager.WIFI_STATE_ENABLED) {
            CheckWifiThread thread = new CheckWifiThread(context);
            thread.start();
        }
        else if(wifistate == WifiManager.WIFI_STATE_DISABLING){
            listener.onWifiStateChange(false);
        }
    }
}

class CheckWifiThread extends Thread{
    Context mContext;
    CheckWifiThread(Context context){
        this.mContext = context;
    }
    @Override
    public void run() {
        // TODO Auto-generated method stub
        ConnectivityManager conMan = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        while(conMan.getActiveNetworkInfo() == null || conMan.getActiveNetworkInfo().getState() != NetworkInfo.State.CONNECTED)
        {
            try
            {
                Thread.sleep(500);
            } catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
        listener.onWifiStateChange(true);
    }
}

}

baidu_19402845
baidu_19402845   2014.12.12 13:07

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**

  • 判断网络连接的工具类
  • 1.网络连接状态
  • 2.WiFi状态
  • 3.移动网状态
  • 4.判断连接类型
  • @author Administrator
  • */
    public class ConnectionUtils {

    /**

    • 判断网络连接状态
    • @param context
    • @return */ public static boolean isNetWorkConnectionUtils(Context context) { if (context != null) { ConnectivityManager manager = (ConnectivityManager) context .getSystemService(context.CONNECTIVITY_SERVICE); NetworkInfo info = manager.getActiveNetworkInfo(); if (info != null) { return info.isAvailable(); } } return false; }

    /**

    • 判断WiFi状态
    • @param context
    • @return */

    public static boolean isWifiConnectionUtils(Context context) {
    if (context != null) {
    ConnectivityManager manager = (ConnectivityManager) context
    .getSystemService(context.CONNECTIVITY_SERVICE);
    NetworkInfo wifiInfo = manager
    .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (wifiInfo != null) {
    return wifiInfo.isAvailable();
    }
    }
    return false;
    }

    /**

    • 判断移动网络
    • @param context
    • @return */ public static boolean isMobelConnectionUtils(Context context) { if (context != null) { ConnectivityManager manager = (ConnectivityManager) context .getSystemService(context.CONNECTIVITY_SERVICE); NetworkInfo mobelInfo = manager .getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if (mobelInfo != null) { return mobelInfo.isAvailable(); } } return false; }

    /**

    • 判断连接类型
    • @param context
    • @return */ public static int getConnectionType(Context context) { if (context != null) { ConnectivityManager manager = (ConnectivityManager) context .getSystemService(context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = manager.getActiveNetworkInfo(); if (netInfo != null && netInfo.isAvailable()) { return netInfo.getType(); } } return -1; } } 最后记得加上权限 这个可以直接在程序中用
luowenlong860502
luowenlong860502   2014.12.12 13:41

可以使用命令 ifconfig eth0 看输出结果是否有ip地址

91program
91program   Ds   Rxr 2014.12.12 11:34

// 当网络变化的时候系统会发出一个广播 Broadcast,只要在程序中注册一个广播接收器 BroadcastReceiver,并在 IntentFilter 中添加相应的过滤,这样一旦网络有变化,程序就能监听到
public static final String CONNECTIVITY_CHANGE_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
private void registerDateTransReceiver() {
Log.i(TAG, "register receiver " + CONNECTIVITY_CHANGE_ACTION);
IntentFilter filter = new IntentFilter();
filter.addAction(CONNECTIVITY_CHANGE_ACTION);
filter.setPriority(1000);
registerReceiver(new MyReceiver(), filter);
}

@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i(TAG, "PfDataTransReceiver receive action " + action);
if(TextUtils.equals(action, CONNECTIVITY_CHANGE_ACTION)){ //网络变化的时候会发送通知
Log.i(TAG, "网络变化了");
return;
}
}

// 当网络变化时,从有网到没网也会发广播,接收到广播的时候要判断当前网络是可用还是不可用状态
public static NetworkInfo getActiveNetwork(Context context){
if (context == null)
return null;
ConnectivityManager mConnMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (mConnMgr == null)
return null;
NetworkInfo aActiveInfo = mConnMgr.getActiveNetworkInfo(); // 获取活动网络连接信息
return aActiveInfo;
}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!