How to determine when a device receives a new IP address?

For multicast purposes, I'm looking for an easy way to determine when an Android IP device changes. How can i do this?

In particular, I want to discover:

  • When a device connects to a new Wi-Fi network and receives an IP address from DHCP
  • When for some reason the device needs to update its IP
+4
source share
1 answer

You can do this with the ConnectivityManager :

You can use this to query the current connection status:

ConnectivityManager connMananger = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = connMananger.getActiveNetworkInfo(); 

The current IP address of the network interfaces can be obtained using NetworkInterface.getNetworkInterfaces ()

And you can get automatic notification when the connection status changes through CONNECTIVITY_ACTION broadcast

+5
source

Source: https://habr.com/ru/post/1387677/


All Articles