Calling a network State check from other activities

Posted by Laurent on Stack Overflow See other posts from Stack Overflow or by Laurent
Published on 2012-09-20T21:21:07Z Indexed on 2012/09/20 21:37 UTC
Read the original article Hit count: 177

I realize this question has been answered before but couldn't find an answer that deals with my specific case.

I want to create a class called "InternetConnectionChecks" that will handle checking a network state and http timeouts. I'll call the methods twice in the app (once at the beginning to get data from a server, and once at the end to send user orders to the server).

For good form I'd like to put all these methods in a single class rather than copy/paste at different points in my code.

To check the network state, I'm using ConnectivityManager; thing is, getSystemService requires a class that extends Activity.

package arbuckle.app;

import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class InternetConnectionChecks extends Activity {

    public boolean isNetworkAvailable(){
        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        if ((activeNetworkInfo != null)&&(activeNetworkInfo.isConnected())){
            return true;
        }else{
            return false;
        }
    }

}

QUESTION: if I call the method isNetworkAvailable from another activity, am I: - going to hit up serious errors. - violating good coding form? *If this isn't the right way to do things, can you point me in the right direction to set up a separate class I can call on to check internet connection?

Thanks everyone!

© Stack Overflow or respective owner

Related posts about android

Related posts about class