ConnectionDetect.class
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class ConnectionDetect
{
private Context context;
public static boolean chechkagain;
public ConnectionDetect(Context context) {
this.context = context;
}
public boolean isConnectingToInternet() {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifi = cm
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo datac = cm
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if ((wifi != null & datac != null)
&& (wifi.isConnected() | datac.isConnected())) {
chechkagain = true;
synchronized (context) {
context.notify();
}
} else {
chechkagain = false;
}
return chechkagain;
}
}
现在检查互联网是否可用
if (connectionDetect.isConnectingToInternet()) {
//Send Email
} else {
Snackbar snackbar = Snackbar
.make(coordinatorLayout,
"Please check internet",
Snackbar.LENGTH_LONG);
snackbar.show();
}
coordinatorLayout 是我们要在其中显示小吃店的 Coordinator Layout 的 id