Ipify-Android
Ipify allows you to get current public IP address when connected to internet in real-time
Add Dependency
Use Gradle:
Step 1: Add it in your root build.gradle
at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Note: In New Android studio updates, now allProjects
block has been removed from root build.gradle
file. So you must add this repository to your root settings.gradle
as below:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
...
maven { url "https://jitpack.io" }
}
}
Step 2: Add the dependency in your module's (e.g. app or any other) build.gradle
file:
dependencies {
...
implementation 'com.github.chintan369:Ipify:<latest-version>'
}
How do I use Ipify-Android?
Step 1
Initialize Ipify in your application class of Android app's onCreate()
method
public class MyApplication : Application() {
...
override fun onCreate() {
super.onCreate();
Ipfy.init(this) // this is a context of application
//or you can also pass IpfyClass type to get either IPv4 address only or universal address IPv4/v6 as
Ipfy.init(this,IpfyClass.IPv4) //to get only IPv4 address
//and
Ipfy.init(this,IpfyClass.UniversalIP) //to get Universal address in IPv4/v6
}
...
}
Step 2
Observe IP address at anywhere in your application. This function will provide you an observer to observe the IP address while changing the network connection from mobile data to Wi-Fi or any other Wi-Fi.
Ipfy.getInstance().getPublicIpObserver().observe(this, { ipData ->
ipData.currentIpAddress // this is a value which is your current public IP address, null if no/lost internet connection
ipData.lastStoredIpAddress // this is a previous IP address while network lost/reconnected and current IP address assigned to null/new one
})
This is how you can get a public IP address without doing any more code!
Feel free to post any issue if you found on this library
If you