For multicast purposes, I’m looking for a simple way to detect when the IP of an Android device changes. How can I go about doing so?
More specifically, I’m looking to detect:
- When the device connects to a new Wifi network and it gets an IP from the DHCP
- When the device for some reason needs to renew an IP
You can do this with the ConnectivityManager:
You can use this to query the current connection state:
The current IP address of network interfaces can be acquired with NetworkInterface.getNetworkInterfaces()
And you can receive automatic notification of when the connection state changes via the CONNECTIVITY_ACTION broadcast