How to block my app if you don’t have internet

Asked

Viewed 95 times

1

I’m creating an app that needs internet, If this person’s mobile phone is without internet the application would not start because it would only open if it was with internet. How does that help someone ?

  • 5

    You know how to see if the phone has internet access?

  • @Guilhermenascimento I think is more related. The main focus is the block when there is no connection. For this I put in the verification response and I know indicated the method Finish to exit the application. = D

  • @acklay I understand, it may be, but is that sometimes it is so small the detail that just follow logic and decide for yourself what will do inside the if, has a lot of question so, that changes small details, usually end up as dup. But let’s see what the people in the analysis queue think :) ... Anyway I voted in your reply for the excellent support and example in Kotlin :D

  • @Guilhermenascimento I also think it’s duplicate, but I didn’t want to use the "hammer" before the AP responded to my comment

  • 1

    @Guilhermenascimento you’re absolutely right, it was a simple detail. I was actually just going to put the condition to finalize the application, and then indicate the question in which the method to query the connection would appear. But then I decided to insert it right here. =]

  • 1

    @acklay usually answer questions that soon after I vote to close, even for the question of quick support, I am not against support, as long as I do not run away from the ideal foundation of the community, which I personally believe is the organization of the content, in case detail for need specifies how you did was ideal really and if you yourself can vote to close even better (depending on the case of the question), examples https://answall.com/search?q=user%3A3635+is%3Aclosed ;)

Show 1 more comment

1 answer

3


A simple way is to let the user enter the application and inside you check if there is a connection. If there is no internet connection, use the method finish() to finish the application. See:

if(!isOnline()){
   finish();
}

A simple method to verify is using the ConnectivityManager:

public boolean isOnline() {
    ConnectivityManager cm = (ConnectivityManager) 
         getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    return netInfo != null && netInfo.isConnected();
}

Here’s a question about test of effective internet connection that maybe the ramaral response can clarify any further details.


In the 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, follow the same JAVA logic.

  • 2

    Perfect! Here is the link to the documentation if you want to include in your reply: getActiveNetworkInfo()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.