Code Monkey home page Code Monkey logo

ethernetcontrol's Introduction

EthernetSetting

以太网设置调用API

功能说明

通过EthernetManager 去设置以太网、获取以太网模式,数据等。Demo可参考ethernetcontroldemo.rar

EthernetManager mEthManager = (EthernetManager) getActivity().getSystemService(Context.ETHERNET_SERVICE);//获取系统以太网管理类

接口说明

1、获取有效的以太网接口。

返回值:以太网名称数组

String[] ifaces = mEthManager.getAvailableInterfaces()
2、根据以太网接口获取以太网模式
IpAssignment mode = mEthManager.getConfiguration(mInterfaceName).getIpAssignment();
3、获取静态IP时的IP信息
StaticIpConfiguration staticIpConfiguration =mEthManager.getConfiguration(mInterfaceName).getStaticIpConfiguration();
4、获取动态Ip时的IP信息

参数说明:mInterfaceName 以太网名称

String iPAddress=mEthManager.getIpAddress(mInterfaceName);//ip地址
String netmask=mEthManager.getNetmask(mInterfaceName);//子网掩码
String gateway=mEthManager.getGateway(mInterfaceName);//网关
String[]dns=mEthManager.getDns(mInterfaceName);//dns
5、设置以太网模式

参数说明: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);
6、打开/关闭以太网

参数说明:enable true:使能 false:关闭

mEthManager.setEnabled(enable);
6、设置广播监听以太网状态
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();
    
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.