android判断当前网络状态,eth wifi pppoe等

利用android标准的ConnectivityManager,利用getNetworkInfo(networkType)获取一下各种网络类型对应的信息

TYPE_ETHERNET //eth
TYPE_WIFI //wifi
TYPE_PPPOE //pppoe

getNetworkInfo(networkType)

public enum State {
CONNECTING, CONNECTED, SUSPENDED, DISCONNECTING, DISCONNECTED, UNKNOWN
}


比如可以通过 
ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

conMan.getNetworkInfo(TYPE_ETHERNET).isConnected() 来检测。

下面是写的case

1     xml 需要添加监控网络权限 <  uses-permission   android:name  =  "android.permission.ACCESS_NETWORK_STATE"  >   uses-permission  >
2   import  android.net.ConnectivityManager;

3   ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.  CONNECTIVITY_SERVICE );
                          if  (conMan.getNetworkInfo(ConnectivityManager.  TYPE_ETHERNET ).isConnected()==  true  ){
                              Log. i(  "test" ,  "ETH is connect ........... " );
                        }
                          if  (conMan.getNetworkInfo(ConnectivityManager.  TYPE_WIFI ).isConnected()==  true  ){
                              Log. i(  "test" ,  "WIFI is connect ........... " );
                        }     

你可能感兴趣的:(Android,源码)