以太网设置调用API
通过EthernetManager 去设置以太网、获取以太网模式,数据等。Demo可参考ethernetcontroldemo.rar
EthernetManager mEthManager = (EthernetManager) getActivity().getSystemService(Context.ETHERNET_SERVICE);//获取系统以太网管理类
返回值:以太网名称数组
String[] ifaces = mEthManager.getAvailableInterfaces()
IpAssignment mode = mEthManager.getConfiguration(mInterfaceName).getIpAssignment();
StaticIpConfiguration staticIpConfiguration =mEthManager.getConfiguration(mInterfaceName).getStaticIpConfiguration();
参数说明:mInterfaceName 以太网名称
String iPAddress=mEthManager.getIpAddress(mInterfaceName);//ip地址
String netmask=mEthManager.getNetmask(mInterfaceName);//子网掩码
String gateway=mEthManager.getGateway(mInterfaceName);//网关
String[]dns=mEthManager.getDns(mInterfaceName);//dns
参数说明:mInterfaceName 以太网名称 mIpConfiguration Ip配置信息
//设置静态Ip
IpConfiguration mIpConfiguration = new IpConfiguration();
mIpConfiguration.setIpAssignment(IpAssignment.STATIC);
mIpConfiguration.setProxySettings(IpConfiguration.ProxySettings.NONE);
mIpConfiguration.setStaticIpConfiguration(mStaticIpConfiguration);
mEthManager.setConfiguration(mInterfaceName, mIpConfiguration);
//设置动态获取
IpConfiguration ipConfiguration = new IpConfiguration();
ipConfiguration.setIpAssignment(IpAssignment.DHCP);
ipConfiguration.setProxySettings(IpConfiguration.ProxySettings.NONE);
mEthManager.setConfiguration(mInterfaceName, ipConfiguration);
参数说明:enable true:使能 false:关闭
mEthManager.setEnabled(enable);
private final static String ETHERNET_ACTION = "android.intent.action.ETHERNET_LINEKSTATE";
...
mIntentFilter = new IntentFilter();
mIntentFilter.addAction(ETHERNET_ACTION);
mIntentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
...
String eth0state = intent.getStringExtra("eth0");
if (!TextUtils.isEmpty(eth0state)) {
if ("up".equals(eth0state)) {
Toast.makeText(mContext, "网线已接上!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(mContext, "网线已断开!", Toast.LENGTH_SHORT).show();
}
}