API for configuring static IP addresses in an Android application

后端 未结 1 912
轻奢々
轻奢々 2020-12-01 09:43

Is it possible to set the IP address of an interface in Android within an application?

I can query the available interfaces and their current addresses using j

相关标签:
1条回答
  • 2020-12-01 10:32

    Settings.System includes several flags you can use for this:

    • WIFI_USE_STATIC_IP
    • WIFI_STATIC_IP
    • WIFI_STATIC_NETMASK
    • WIFI_STATIC_GATEWAY
    • WIFI_STATIC_DNS1 and WIFI_STATIC_DNS2

    You'll also need the android.permission.WRITE_SETTINGS permission declared for your application.

    Then in your activity:

    final ContentResolver cr = getContentResolver();
    Settings.System.putInt(cr, Settings.System.WIFI_USE_STATIC_IP, 1);
    Settings.System.putString(cr, Settings.System.WIFI_STATIC_IP, "you.re.ip.addr");
    // call putString() for each value to set for your interface
    

    If you want to change the IP address of the carrier's 3G/4G,etc interface, I do not believe this is possible - as it is connected to the carrier and uses their DHCP/security for enabling you to connect and use their services (sort of like changing the external IP of your cable modem without the consent of your ISP).

    0 讨论(0)
提交回复
热议问题