变化的互联网代理(WIFI,3G,GPRS)的安卓2.2从code上 [英] change internet proxy (Wifi,3G,GPRS) in android 2.2 an upper from code

查看:128
本文介绍了变化的互联网代理(WIFI,3G,GPRS)的安卓2.2从code上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改代理从code所有的互联网连接类型(WIFI,3G,GPRS)(有一个按钮事件)。过了几分钟,他们改变是什么。 我搜索,知道,变革代理从code设定我需要这个权限:

I'm trying to change proxy for all internet Connection type (Wifi,3G,GPRS) from code (with a button event). and after a couple of minutes change them to what they were. I Searched and know that for change proxy setting from code I need this permissions :

 <uses-permission android:name="android.permission.WRITE_SETTINGS" />
 <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />

但我不能让那个权限我的程序。 我希望我为Android 2.2和上层应用程序的工作。 你能给我一个示例项目与Android工作室?

but I cant get that permissions to my program. I want my app work for android 2.2 and upper. Can you give me a sample project with android studio?

推荐答案

您可以参考以下code通过增加新的无线网络设置的反射,[我有写这篇code和测试的4.x版]

You can refer below code for adding new wifi setting via reflection, [i have written this code and test on 4.x]

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    connectToAP("12345", "12345");

    WifiConfiguration wifiConf = null;
    WifiManager wifiManager = (WifiManager) getSystemService(MainActivity.WIFI_SERVICE);
    WifiInfo connectionInfo = wifiManager.getConnectionInfo();
    List<WifiConfiguration> configuredNetworks = wifiManager
    .getConfiguredNetworks();
    for (WifiConfiguration conf : configuredNetworks) {
    if (conf.networkId == 13) {
    wifiConf = conf;
    try {
    setIpAssignment("STATIC", wifiConf); // or "DHCP" for
    // dynamic setting
    setIpAddress(InetAddress.getByName("192.168.0.100"), 24,
    wifiConf);
    setGateway(InetAddress.getByName("4.4.4.4"), wifiConf);
    setDNS(InetAddress.getByName("4.4.4.4"), wifiConf);
    wifiManager.updateNetwork(wifiConf); // apply the setting
    wifiManager.saveConfiguration(); // Save it
    } catch (Exception e) {
    e.printStackTrace();
    }
    break;
    }
    }

    }

    public static void setIpAssignment(String assign, WifiConfiguration wifiConf)
    throws SecurityException, IllegalArgumentException,
    NoSuchFieldException, IllegalAccessException {
    setEnumField(wifiConf, assign, "ipAssignment");
    }

    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));
    }

    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;
    }

    @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;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
    return true;
    }
    return super.onOptionsItemSelected(item);
    }

    String TAG = "wifi";
    WifiManager wifiManager;

    public void connectToAP(String ssid, String passkey) {
    Log.i(TAG, "* connectToAP");
    wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
    WifiConfiguration wifiConfiguration = new WifiConfiguration();

    String networkSSID = ssid;
    String networkPass = passkey;

    Log.d(TAG, "# password " + networkPass);

    // for (ScanResult result : scanResultList) {
    // if (result.SSID.equals(networkSSID)) {
    if (true) {
    // String securityMode = getScanResultSecurity(result);
    String securityMode = "WEP";
    if (securityMode.equalsIgnoreCase("OPEN")) {

    wifiConfiguration.SSID = "\"" + networkSSID + "\"";
    wifiConfiguration.allowedKeyManagement
    .set(WifiConfiguration.KeyMgmt.NONE);
    int res = wifiManager.addNetwork(wifiConfiguration);
    Log.d(TAG, "# add Network returned " + res);

    boolean b = wifiManager.enableNetwork(res, true);
    Log.d(TAG, "# enableNetwork returned " + b);

    wifiManager.setWifiEnabled(true);

    } else if (securityMode.equalsIgnoreCase("WEP")) {

    wifiConfiguration.SSID = "\"" + networkSSID + "\"";
    wifiConfiguration.wepKeys[0] = "\"" + networkPass + "\"";
    wifiConfiguration.wepTxKeyIndex = 0;
    wifiConfiguration.allowedKeyManagement
    .set(WifiConfiguration.KeyMgmt.NONE);
    wifiConfiguration.allowedGroupCiphers
    .set(WifiConfiguration.GroupCipher.WEP40);
    int res = wifiManager.addNetwork(wifiConfiguration);
    Log.d(TAG, "### 1 ### add Network returned " + res);

    boolean b = wifiManager.enableNetwork(res, true);
    Log.d(TAG, "# enableNetwork returned " + b);

    wifiManager.setWifiEnabled(true);
    }

    wifiConfiguration.SSID = "\"" + networkSSID + "\"";
    wifiConfiguration.preSharedKey = "\"" + networkPass + "\"";
    wifiConfiguration.hiddenSSID = true;
    wifiConfiguration.status = WifiConfiguration.Status.ENABLED;
    wifiConfiguration.allowedGroupCiphers
    .set(WifiConfiguration.GroupCipher.TKIP);
    wifiConfiguration.allowedGroupCiphers
    .set(WifiConfiguration.GroupCipher.CCMP);
    wifiConfiguration.allowedKeyManagement
    .set(WifiConfiguration.KeyMgmt.WPA_PSK);
    wifiConfiguration.allowedPairwiseCiphers
    .set(WifiConfiguration.PairwiseCipher.TKIP);
    wifiConfiguration.allowedPairwiseCiphers
    .set(WifiConfiguration.PairwiseCipher.CCMP);
    wifiConfiguration.allowedProtocols
    .set(WifiConfiguration.Protocol.RSN);
    wifiConfiguration.allowedProtocols
    .set(WifiConfiguration.Protocol.WPA);

    int res = wifiManager.addNetwork(wifiConfiguration);
    Log.d(TAG, "### 2 ### add Network returned " + res);

    wifiManager.enableNetwork(res, true);

    boolean changeHappen = wifiManager.saveConfiguration();

    if (res != -1 && changeHappen) {
    Log.d(TAG, "### Change happen");

    // AppStaticVar.connectedSsidName = networkSSID;

    } else {
    Log.d(TAG, "*** Change NOT happen");
    }

    wifiManager.setWifiEnabled(true);
    }
    // }
    }

    public String getScanResultSecurity(ScanResult scanResult) {
    Log.i(TAG, "* getScanResultSecurity");

    final String cap = scanResult.capabilities;
    final String[] securityModes = { "WEP", "PSK", "EAP" };

    for (int i = securityModes.length - 1; i >= 0; i--) {
    if (cap.contains(securityModes[i])) {
    return securityModes[i];
    }
    }

    return "OPEN";
    }
}

这篇关于变化的互联网代理(WIFI,3G,GPRS)的安卓2.2从code上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆