Android应用开发Android 获取手机Wifi地址和Gprs地址,反射修改Wifi地址
白羽 2019-03-12 来源 :网络 阅读 1241 评论 0

摘要:本文将带你了解Android应用开发Android 获取手机Wifi地址和Gprs地址,反射修改Wifi地址,希望本文对大家学Android有所帮助。

    本文将带你了解Android应用开发Android 获取手机Wifi地址和Gprs地址,反射修改Wifi地址,希望本文对大家学Android有所帮助。



Android应用开发Android 获取手机Wifi地址和Gprs地址,反射修改Wifi地址



    Android 获取手机Wifi地址和Gprs地址,反射修改Wifi地址。
   
   
    //查看Wifi地址 public String   getWifiIpAddress() { //获取wifi服务   WifiManager wifiManager = (WifiManager)   getSystemService(Context.WIFI_SERVICE);   //判断wifi是否开启 if   (!wifiManager.isWifiEnabled()) { wifiManager.setWifiEnabled(true);   } WifiInfo wifiInfo = wifiManager.getConnectionInfo(); int   ipAddress = wifiInfo.getIpAddress(); String ip =   intToIp(ipAddress); Log.d(wuc,   wifiIp =    + ip); return ip; }   //获取GPRS本地ip地址 public   String getLocalIpAddress() { try   { for   (Enumerationen = NetworkInterface   .getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf =   en.nextElement(); for   (EnumerationenumIpAddr = intf   .getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress   = enumIpAddr.nextElement(); if   (!inetAddress.isLoopbackAddress() &&   !inetAddress.isLinkLocalAddress()) { Log.d(wuc,   gprs =  +   inetAddress.getHostAddress().toString()); return   inetAddress.getHostAddress().toString();   }

                }
            }
        } catch   (SocketException ex) {
              Log.e(WifiPreference IpAddress,   ex.toString());
        }
        return   null;
    }
     
    private String intToIp(int   i) {
     
        return   (i & 0xFF ) + . +
                ((i >> 8 ) &   0xFF) + . +
                ((i >> 16 ) &   0xFF) + . +
                ( i >> 24 &   0xFF) ;
    }

 
    日志信息

    09-10 14:58:06.288 12190-12190/com.example.administrator.newtext D/wuc:   gprs = 10.96.76.60
    09-10 14:58:15.488 12190-12190/com.example.administrator.newtext D/wuc:   wifiIp = 192.168.1.103
 
    修改wifi地址网上找的-------------------------
 
    @Override public void onCreate(Bundle savedInstanceState)   { super.onCreate(savedInstanceState);   setContentView(R.layout.activity_main);

    wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);   WifiInfo connectionInfo =   wifiManager.getConnectionInfo();   ListconfiguredNetworks = wifiManager   .getConfiguredNetworks(); for   (WifiConfiguration conf : configuredNetworks) {   if (conf.networkId   == connectionInfo.getNetworkId()) { wifiConf   = conf; Toast.makeText(this,   11111,   Toast.LENGTH_SHORT).show();   break; } }   Log.d(wuc, wifiConfout =  +   wifiConf); try {   setIpAssignment(STATIC,   wifiConf);   setIpAddress(InetAddress.getByName(192.168.0.110),   24, wifiConf);   setGateway(InetAddress.getByName(255.255.255.0),   wifiConf);   setDNS(InetAddress.getByName(255.255.255.0),   wifiConf); } catch   (Exception e) { //   TODO Auto-generated catch block     e.printStackTrace(); }   Toast.makeText(this,   1121, Toast.LENGTH_SHORT).show();   wifiManager.updateNetwork(wifiConf);   // apply the setting
 
    public static void setIpAssignment(String   assign, WifiConfiguration wifiConf) throws   SecurityException, IllegalArgumentException,   NoSuchFieldException, IllegalAccessException {   Log.d(wuc, wifiConfin =  + wifiConf);   setEnumField(wifiConf, assign, ipAssignment); }   public static void setIpAddress(InetAddress   addr, int prefixLength, WifiConfiguration   wifiConf) throws SecurityException,   IllegalArgumentException, NoSuchFieldException, IllegalAccessException,   NoSuchMethodException, ClassNotFoundException, InstantiationException,   InvocationTargetException { Object linkProperties = getField(wifiConf,   linkProperties); if   (linkProperties == null)   return; Class laClass =   Class.forName(android.net.LinkAddress);   Constructor laConstructor = laClass.getConstructor(new   Class[]{ InetAddress.class,   int.class}); Object   linkAddress = laConstructor.newInstance(addr, prefixLength); ArrayList   mLinkAddresses = (ArrayList) getDeclaredField(linkProperties,   mLinkAddresses); mLinkAddresses.clear(); mLinkAddresses.add(linkAddress);   } public static void setGateway(InetAddress   gateway, WifiConfiguration wifiConf) throws   SecurityException, IllegalArgumentException,   NoSuchFieldException, IllegalAccessException, ClassNotFoundException,   NoSuchMethodException, InstantiationException, InvocationTargetException {   Object linkProperties = getField(wifiConf,   linkProperties); if   (linkProperties == null)   return; Class routeInfoClass =   Class.forName(android.net.RouteInfo);   Constructor routeInfoConstructor = routeInfoClass   .getConstructor(new Class[]{InetAddress.class});   Object routeInfo = routeInfoConstructor.newInstance(gateway); ArrayList   mRoutes = (ArrayList) getDeclaredField(linkProperties,   mRoutes); mRoutes.clear(); mRoutes.add(routeInfo);   } public static void setDNS(InetAddress dns,   WifiConfiguration wifiConf) throws   SecurityException, IllegalArgumentException,   NoSuchFieldException, IllegalAccessException { Object linkProperties =   getField(wifiConf, linkProperties);   if (linkProperties ==   null) return;   ArrayListmDnses = (ArrayList)   getDeclaredField( linkProperties, mDnses);   mDnses.clear(); // or add a new dns address , here I just want to   // replace DNS1   mDnses.add(dns); } public static   Object getField(Object obj, String name) throws   SecurityException, NoSuchFieldException,   IllegalArgumentException, IllegalAccessException { Field f =   obj.getClass().getField(name); Object out = f.get(obj); return   out; } public static Object   getDeclaredField(Object obj, String name) throws   SecurityException, NoSuchFieldException, IllegalArgumentException,   IllegalAccessException { Field f = obj.getClass().getDeclaredField(name);   f.setAccessible(true); Object out = f.get(obj);   return out; } public static void   setEnumField(Object obj, String value, String name)   throws SecurityException, NoSuchFieldException,   IllegalArgumentException, IllegalAccessException {   Log.d(wuc, wifiConfinin =  + obj); Field f =   obj.getClass().getField(name); f.set(obj, Enum.valueOf((Class)   f.getType(), value));   }
    }
 
    但是这个wifi修改地址没成功。
 
    提示:java.lang.NoSuchFieldException: ipAssignment
 
    难道是ipAssignment名称不对?
 
    看网上说getField换成getDeclaredField
  
    提示:java.lang.NoSuchFieldException: No field ipAssignment in class   Landroid/net/wifi/WifiConfiguration; (declaration of   'android.net.wifi.WifiConfiguration' appears in   /system/framework/framework.jar)
        

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之Android频道!

本文由 @白羽 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程