Answer the question
In order to leave comments, you need to log in
How to check for a network connection at any point in an android application?
Hello friends, I have been writing applications for a long time, but the habit is the same as I did and forgot, I would like to remember, or maybe someone suggested a better option.
I want to create a checker that would check the status of connecting to the phone's network, at any point in the application. Accordingly, I would perform some actions on this, that is, without unnecessary initialization, create, extend some kind of base class and declare a function in it to check, which in turn would do this, or maybe apply the observer pattern to the onCreate event, I’m right now I don’t even know how to listen to it, who will tell you the correct option?
Answer the question
In order to leave comments, you need to log in
I usually create a helper class and there is a method:
public static boolean isOnline(Context context)
ConnectivityManager cman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ninfo = cmn.getActiveNetworkInfo();
if(ninfo == null || !ninfo.isConnectedOrConnecting())
return false;
return true;
if(HelperClass.isOnline(this)) //I'm online
else //Sorry
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question