Android静态IP设置

import android.content.Context;
import android.util.Log;

import java.util.List;

/**
 * 描述:
 * 作者:create by Administrator on 2017/8/4 15:00
 */

public class EthernetUtil {
    private static final String TAG = "EthernetUtil";

    private static ClassReflectionWraper mEthernetWraper;
    private static ClassReflectionWraper mEthernetDeviceWraper;
    private static Object mEthernetInstance;
    private static Object mEthernetDevice;
    private static boolean mPreEthernetStatus;


    /**
     * @param context
     *
     *            context needed for initialize;
     */
    public static void initialize(Context context) {

        mEthernetDeviceWraper = new ClassReflectionWraper(
                "android.net.ethernet.EthernetDevInfo", false);
        mEthernetWraper = new ClassReflectionWraper(
                "android.net.ethernet.EthernetManager", false);
        mEthernetInstance = mEthernetWraper.invokeMethods("getInstance", null, null);
        getDefaultEthernetStatus();
        getDefaultEthernetDevice();

    }


    /**
     * warning: initialize before invoke this method
     *
     */
    private static void getDefaultEthernetDevice() {
        List<Object> lists = (List<Object>) mEthernetWraper.invokeMethods("getDeviceNameList", mEthernetInstance, null);
        Log.d(TAG, "getDefaultEthernetDevice lists:" + lists);
        if (null != lists && !lists.isEmpty()) {
            Log.d(TAG, "getDefaultEthernetDevice lists.size:" + lists.size());
            mEthernetDevice = lists.get(0);
        }
    }

    /**
     * warning: initialize before invoke this method
     */
    private static void getDefaultEthernetStatus() {
        mPreEthernetStatus = isEthernetOn();
    }


    /**
     * warning: initialize before invoke this method
     */
    public static boolean isEthernetOn() {
        if (null == mEthernetInstance) {
            Log.e("EthernetUtil", "isEthernetOn, mEthernetInstance is null");
            return false;
        }
        if (null == mEthernetWraper) {
            Log.e("EthernetUtil", "isEthernetOn, mEthernetWraper is null");
            return false;
        }
        return (Boolean) mEthernetWraper.invokeMethods("isOn", mEthernetInstance, null);
    }


    /**
     * warning: initialize before invoke this method
     */
    public static boolean isEthernetConfigured() {
        return (Boolean) mEthernetWraper.invokeMethods("isConfigured", mEthernetInstance, null);
    }


    public static int getConnectMode() {
        return (Integer) mEthernetDeviceWraper.invokeMethods("getConnectMode", mEthernetDevice, null);
    }


    public static String getIpAdress() {
        return (String) mEthernetDeviceWraper.invokeMethods("getIpAddress", mEthernetDevice, null);
    }


    public static String getGateway() {
        return (String) mEthernetDeviceWraper.invokeMethods("getGateWay", mEthernetDevice, null);
    }


    public static String getDns() {
        return (String) mEthernetDeviceWraper.invokeMethods("getDnsAddr", mEthernetDevice, null);
    }
    public static String getMask() {
        return (String) mEthernetDeviceWraper.invokeMethods("getNetMask", mEthernetDevice, null);
    }


    public static int getState() {
        return (Integer) mEthernetWraper.invokeMethods("getState", mEthernetInstance);
    }


    public static void disableDhcp() {
        mEthernetDeviceWraper.invokeMethods("setConnectMode", mEthernetDevice, 0);
        mEthernetWraper.invokeMethods("updateDevInfo", mEthernetInstance, mEthernetDevice);
    }


    public static void enableDhcp() {
        mEthernetDeviceWraper.invokeMethods("setConnectMode", mEthernetDevice, 1);
        mEthernetWraper.invokeMethods("updateDevInfo", mEthernetInstance, mEthernetDevice);
    }


    public static void useStaticAddress(String ipAddress, String gateway, String dns, String mask) {
        Log.d(TAG, "useStaticAddress ipAddress:" + ipAddress + ", gateway:" + gateway + ", dns:" + dns
                + ", mask:" + mask);
        mEthernetDeviceWraper.invokeMethods("setConnectMode", mEthernetDevice, 0);
        mEthernetDeviceWraper.invokeMethods("setIpAddress", mEthernetDevice, ipAddress);
        mEthernetDeviceWraper.invokeMethods("setNetMask", mEthernetDevice, mask);
        mEthernetDeviceWraper.invokeMethods("setGateWay", mEthernetDevice, gateway);
        mEthernetDeviceWraper.invokeMethods("setDnsAddr", mEthernetDevice, dns);
        mEthernetWraper.invokeMethods("updateDevInfo", mEthernetInstance, mEthernetDevice);

    }


    /**
     * @param enalbe
     *            set ethernet status; warning: initialize before invoke this
     *            method
     */
    public static void setEthernetEnable(boolean enalbe) {
        mEthernetWraper.invokeMethods("updateDevInfo", mEthernetInstance, mEthernetDevice);
        mEthernetWraper.invokeMethods("setEnabled", mEthernetInstance, enalbe);
    }


    /**
     * warning: initialize before invoke this method
     */
    public static void restoreEethernetStatus() {
        setEthernetEnable(mPreEthernetStatus);
    }


    public static void updateNetwork() {
        mEthernetWraper.invokeMethods("updateDevInfo", mEthernetInstance, mEthernetDevice);
    }
    
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值