How To Detect When User Connects To Wifi Or Mobile Internet In Android
I want to check for particular update when user each time connects to internet.I tried the following codes : unfortunately app is getting stopped while checking for network Broadca
Solution 1:
Once I had implemented a check in my app, to see if there is any internet connection or not. You can have a look at this --
publicclassConnectionDetector {
private Context context;
publicConnectionDetector(Context cont){
this.context = cont;
}
publicbooleanisConnectingToInternet(){
ConnectivityManagerconnectivity= (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (inti=0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
returntrue;
}
}
}
}
returnfalse;
}
}
You can initialize an object of this class in your OnCreate method.
And finally call this class' method just before you upload files.
BooleanisInternetConnected= cd.isConnectingToInternet();
if (isInternetConnected)
{
//perform your job here.
}
EDITED::
NetworkInfoactiveNetworkInfo= connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
Hope this helps.
Post a Comment for "How To Detect When User Connects To Wifi Or Mobile Internet In Android"