I am working on a live project. and when user click on the app. the welcome screen appears(there is a webview on that screen). and if the internet is not connected then the
This cup of code helps you to check internet connection available or not from the API level 16 to 30
@SuppressWarnings("deprecation")
public static boolean isInternetAvailable(Activity activity) {
ConnectivityManager conMgr = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Network network = conMgr.getActiveNetwork();
NetworkCapabilities networkCapabilities = conMgr.getNetworkCapabilities(network);
if (networkCapabilities != null) {
return networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
} else return false;
} else {
// below API Level 23
return conMgr.getActiveNetworkInfo() != null
&& conMgr.getActiveNetworkInfo().isAvailable()
&& conMgr.getActiveNetworkInfo().isConnected();
}
}
I've tried different methods described above, besides it I've been caught in some cases. So finally, I'm using below code snippet to make a network call...
try {
InetAddress address = InetAddress.getByName("www.stackoverflow.com");
//Connected to working internet connection
} catch (UnknownHostException e) {
e.printStackTrace();
//Internet not available
}
This can be useful in many perspectives. If something is not fine in this approach, please let me know.
if (InternetConnection.checkConnection(context)) {
// Internet Available...
} else {
// Internet Not Available...
}
just copy below class
public class InternetConnection {
/** CHECK WHETHER INTERNET CONNECTION IS AVAILABLE OR NOT */
public static boolean checkConnection(Context context) {
final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connMgr.getActiveNetworkInfo();
if (activeNetworkInfo != null) { // connected to the internet
Toast.makeText(context, activeNetworkInfo.getTypeName(), Toast.LENGTH_SHORT).show();
if (activeNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
// connected to wifi
return true;
} else if (activeNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
// connected to the mobile provider's data plan
return true;
}
}
return false;
}
}
Give following permission to manifest
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Internet connection also check either in onCreate()
or onResume()
method. There is no need to call inside the AsyncTask.
or You can call before call the AsyncTask execute() method
if(isOnline)
{
MyAsyncTask task=new MyAMyAsyncTask();
task.execute();
}
else
{
// no internet connection
}
Check internet connection
ConnectivityManager mgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = mgr.getActiveNetworkInfo();
if (netInfo != null) {
if (netInfo.isConnected()) {
// Internet Available
}else {
//No internet
}
} else {
//No internet
}
doInBackground
runs on a different Thread than the main UI, so you can't create a show a dialog here. Instead, override onPreExecute in your AsyncTask
, and do the test there.