Found the best solution . All the beauty in isConnectedOrConnecting (); - it works as it should in general! Those. the problem " The fact is that wi-fi does not connect in a split second, I just tested it myself. Ie, as soon as they clicked on wi-fi, at first he would still say that the Internet is not connected, but as soon as wi-fi feels data exchange, then voila the Internet is connected " - was solved . And do not make the delay artificial.
one.
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo; public class ConnectivityReceiver extends BroadcastReceiver { public static ConnectivityReceiverListener connectivityReceiverListener; public ConnectivityReceiver() { super(); } @Override public void onReceive(Context context, Intent arg1) { ConnectivityManager cm = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting(); if (connectivityReceiverListener != null) { connectivityReceiverListener.onNetworkConnectionChanged(isConnected); } } public static boolean isConnected() { ConnectivityManager cm = (ConnectivityManager) MyApplication.getInstance().getApplicationContext() .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); return activeNetwork != null && activeNetwork.isConnectedOrConnecting(); } public interface ConnectivityReceiverListener { void onNetworkConnectionChanged(boolean isConnected); } }
2
import android.app.Application; public class MyApplication extends Application { private static MyApplication mInstance; @Override public void onCreate() { super.onCreate(); mInstance = this; } public static synchronized MyApplication getInstance() { return mInstance; } public void setConnectivityListener(ConnectivityReceiver.ConnectivityReceiverListener listener) { ConnectivityReceiver.connectivityReceiverListener = listener; } }
3
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="info.androidhive.checkinternet"> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <application android:name=".MyApplication" ...> ... <receiver android:name=".ConnectivityReceiver" android:enabled="true"> <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> </intent-filter> </receiver> </application> </manifest>
four.
private void checkConnection() { boolean isConnected = ConnectivityReceiver.isConnected(); showSnack(isConnected); } private void showSnack(boolean isConnected) { String message; int color; if (isConnected) { message = "Good! Connected to Internet"; color = Color.WHITE; } else { message = "Sorry! Not connected to internet"; color = Color.RED; } Snackbar snackbar = Snackbar .make(findViewById(R.id.fab), message, Snackbar.LENGTH_LONG); View sbView = snackbar.getView(); TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text); textView.setTextColor(color); snackbar.show(); } @Override protected void onResume() { super.onResume(); MyApplication.getInstance().setConnectivityListener(this); } @Override public void onNetworkConnectionChanged(boolean isConnected) { showSnack(isConnected); }
Ps in the video link where you can clearly see how it works perfectly.