android 4.0 无线ip修改

package com.example.ipconfig;


import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;


import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Toast;


// 运行之后ip修改为192.168.7.103
public class MainActivity extends Activity {


/** Called when the activity is first created. */
// 定义侦听端口号
final int SERVER_PORT = 30000;


public static void setIpAssignment(String assign, WifiConfiguration wifiConf)
throws SecurityException, IllegalArgumentException,
NoSuchFieldException, IllegalAccessException {
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;


ArrayList<InetAddress> mDnses = (ArrayList<InetAddress>) 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 {
Field f = obj.getClass().getField(name);
f.set(obj, Enum.valueOf((Class<Enum>) f.getType(), value));
}


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WifiConfiguration wifiConf = null;
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo connectionInfo = wifiManager.getConnectionInfo();


List<WifiConfiguration> configuredNetworks = wifiManager
.getConfiguredNetworks();
for (WifiConfiguration conf : configuredNetworks) {
if (conf.networkId == connectionInfo.getNetworkId()) {
wifiConf = conf;
Toast.makeText(this, "11111", 5555).show();
break;
}
}


try {
setIpAssignment("STATIC", wifiConf);


setIpAddress(InetAddress.getByName("192.168.7.103"), 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", 5555).show();
wifiManager.updateNetwork(wifiConf); // apply the setting 更新ip


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


}   经过测试 完全没有问题
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值