There is a BroadcastReceiver, which should work if the connection to WiFi is lost. It should be:

The application is running - the WiFi is turned off - the onReceive () is being processed - the process is terminated and the user is notified of this.

Together with the response to WiFi disconnection, the onReceive () method works in the following scenario: The application was launched with WiFi turned off — WiFi was turned on — the onReceive () method also works. Ie, it turns out that WiFi is on, and the user sees a message about the opposite. How to fix this? .

public class WifiReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo ni = cm.getActiveNetworkInfo(); if (!(ni != null && ni.getType() == ConnectivityManager.TYPE_WIFI)) { context.stopService(new Intent(context, NotificationService.class)); Intent ss = new Intent(context, MainActivity.class); ss.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); ss.putExtra("noWifi", "noWifi"); context.startActivity(ss); } } } 

Manifesto:

 <receiver android:name=".WifiReceiver" > <intent-filter android:priority="100"> <action android:name="android.net.wifi.WIFI_STATE_CHANGED"/> </intent-filter> </receiver> 
  • The fact is that wi-fi does not connect in a split second, I just tested it myself. Those. as soon as they clicked on wi-fi, at first, it will still say that the Internet is not connected, but as soon as wi-fi starts to exchange data, then voila Internet is connected - iFr0z
  • And in order to check this situation visually TYPE_WIFI is not enough, add another TYPE_NOT_CONNECTED and immediately everything will fall into place - iFr0z
  • @iFr0z thanks. But how then to be in my case? To make a delay of 5-10 seconds, what would give WiFi connect - a reasonable solution? - Haze
  • Do not believe it, I myself am worried about it now: DDD delay is a very nasty decision because the devices are different and their power unfortunately is also: (i.e., the hell do you need to jump the seconds you need ... - iFr0z

1 answer 1

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.

  • Gorgeous! It works as it should. Thank you) - Haze
  • @Haze was glad to help!) Do not forget to tick off the answer :) - iFr0z
  • I would love to, but “Thank you for your feedback! Your votes will influence the rating of messages as soon as your reputation exceeds the threshold of 15 points” - Haze
  • @Haze aaaa, exactly) - iFr0z
  • Well, that is what I meant, I just did not remember the method or the flag. - AZ