Max Asura
Max Asura

Reputation: 821

check internet activity

I done an app that uses a webview to listen audio from internet. Now, I add a method (isOnline) to check if there's (or not) an internet activity. I've a doubt: I can use this method running always in OnCreate?

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

Upvotes: 0

Views: 100

Answers (1)

Sebastian
Sebastian

Reputation: 236

If you block the execution of the onCreate() aka the UI thread will be blocked and your app will throw ANR (Application Not Responding) exception.

However your method doesn't tell you that you are connected to the interned, it just tell you that your device isConnectedOrConnecting to nearest router. What is after that you don't know.

For check real internet connection you need to make a request to a server, such as www.google.com and if you get the answer then you have the connection live.

And another thing, you don't need to monitor your connection continuously. In the most simple manner you can do this in a background thread from time to time, but only if you cannot get events from your radio player.

Upvotes: 1

Related Questions