当前位置: 代码迷 >> 综合 >> Android静态IP设置
  详细解决方案

Android静态IP设置

热度:52   发布时间:2023-10-12 04:38:00.0
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);}}
  相关解决方案