yydl
yydl

Reputation: 24474

How can I detect when the device gets a new IP?

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:

Upvotes: 6

Views: 8379

Answers (1)

Thomas Dignan
Thomas Dignan

Reputation: 7102

You can do this with the ConnectivityManager:

You can use this to query the current connection state:

ConnectivityManager connMananger = (ConnectivityManager) 
            context.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo netInfo = connMananger.getActiveNetworkInfo();

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

Upvotes: 6

Related Questions