APK:ethernet广播监听以太网状态、ping外网、广播监听wifi的状态
一.LAN/WAN/WLAN/WWAN区别总结
LAN: Local Area Network : 局域网
WAN: Wide Area Network, 广域网
WLAN: Wireless LAN, 无线局域网
WWAN: Wireless WAN: 无线广域网
二.以太网监听广播
2.1.AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
2.2.MyBroadcast.java
package com.gatsby.networktest; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.util.Log; public class MyBroadcast extends BroadcastReceiver { static final String BOOT_COMPLETED = "android.intent.action.BOOT_COMPLETED"; public int NET_ETHERNET = 1; public int NET_WIFI = 2; public int NET_NOCONNECT = 0; @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub String action = intent.getAction(); if (action.equals(BOOT_COMPLETED)) { Intent startService = new Intent(context, NetworkService.class); Log.d("gatsby", "BOOT_COMPLETED Broadcast-----------!!!!"); context.startService(startService); } else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION) || action.equals("android.net.conn.CONNECTIVITY_CHANGE")) { switch (isNetworkAvailable(context)) { case 1: Log.d("gatsby", "-----------networktest---------ethernetState"); Intent etherIntent = new Intent(); etherIntent.setAction("com.xinhua.etherIntent"); context.sendBroadcast(etherIntent); break; case 2: Log.d("gatsby", "-----------networktest---------wifinetState"); Intent wifiIntent = new Intent(); wifiIntent.setAction("com.xinhua.wifiIntent"); context.sendBroadcast(wifiIntent); break; case 0: Log.d("gatsby", "-----------networktest---------NONO"); break; default: break; } } } private int isNetworkAvailable(Context mcontext) { ConnectivityManager connectMgr = (ConnectivityManager) mcontext.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo ethNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET); NetworkInfo wifiNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (ethNetInfo != null && ethNetInfo.isConnected()) { return NET_ETHERNET; } else if (wifiNetInfo != null && wifiNetInfo.isConnected()) { return NET_WIFI; } else { return NET_NOCONNECT; } } }
三.节点判断以太网插拔状态 cat /sys/class/net/eth0/carrier
四.ping 外网
public static final boolean ping() { String result = null; try { String ip = "www.baidu.com"; Process p = Runtime.getRuntime().exec("ping -c 3 -w 100 " + ip); int status = p.waitFor(); if (status == 0) { result = "success"; return true; } else { result = "failed"; } } catch (IOException e) { result = "IOException"; } catch (InterruptedException e) { result = "InterruptedException"; } finally { Log.d("gatsby", " ping result = " + result); } return false; }
五.wifi网卡状态、操作网卡的权限
5.1.wifi网卡状态
a.WIFI_STATE_DISABLED:WIFI网卡不可用
b.WIFI_STATE_DISABLING:WIFI正在关闭
c.WIFI_STATE_ENABLED:WIFI网卡可用
d.WIFI_STATE_ENABLING:WIFI网卡正在打开
e.WIFI_STATE_UNKNOWN:未知网卡状态
5.2.demo
a.AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
b.MyBroadcast
package com.gatsby.wifienable; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.net.wifi.WifiManager; import android.util.Log; public class MyBroadcast extends BroadcastReceiver { Context mContext; WifiManager mWifiManager; @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub this.mContext = context; if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) { int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0); Log.d("gatsby", "wifiState->" + wifiState); //wifi 开关 mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE); mWifiManager.setWifiEnabled(false); switch (wifiState) { case WifiManager.WIFI_STATE_DISABLED: Log.d("gatsby", " WIFI_STATE_DISABLED " + wifiState); break; case WifiManager.WIFI_STATE_DISABLING: Log.d("gatsby", " WIFI_STATE_DISABLING " + wifiState); break; case WifiManager.WIFI_STATE_ENABLED: Log.d("gatsby", " WIFI_STATE_ENABLED " + wifiState); break; case WifiManager.WIFI_STATE_ENABLING: Log.d("gatsby", " WIFI_STATE_ENABLING " + wifiState); break; case WifiManager.WIFI_STATE_UNKNOWN: Log.d("gatsby", " WIFI_STATE_UNKNOWN " + wifiState); break; } } } }