I'm creating an application that needs internet, If this person's cell phone is without internet, the application would not start because it would only open if you were on the internet. How can someone help you?
I'm creating an application that needs internet, If this person's cell phone is without internet, the application would not start because it would only open if you were on the internet. How can someone help you?
A simple way is to let the user enter the application and inside you to check if the connection exists. If there is no internet connection, use the finish()
method to terminate the application. See:
if(!isOnline()){
finish();
}
A simple method to check is using ConnectivityManager
:
public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
return netInfo != null && netInfo.isConnected();
}
Here you have a question about internet connection test effective that maybe Ramaral's answer can shed some light on the details.
Kotlin would look like this:
fun isOnline(): Boolean {
val cm = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val netInfo = cm.activeNetworkInfo
return netInfo != null && netInfo.isConnected
}
To use this function in Kotlin, it follows the same logic as JAVA.