-1

I want to replicate the status bar behavior on my own when connected to WiFi.

I've registered a receiver, but i don't know what exact action i suppose to listen to be able to detect if I am connected to WiFi AP, but it has no internet connection on it (hotspot disconnected).

I was trying with:

 override fun onReceive(context: Context, intent: Intent) {
  val wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, DEFAULT_WIFI_STATE_VALUE)
  wifiStateSubject.onNext(wifiState)
  Timber.d("Current wifi state: $wifiState")
}

But it doesn't get me any valuable information when i was connected to my hotspot which was disconnected from the network.

I want to be able to detect and show exclamation mark on my own when i am connected to WiFi AP, but no internet connection.

Is there a specific action to which i should register with my receiver using WiFiManager action?

Community
  • 1
  • 1
K.Os
  • 5,123
  • 8
  • 40
  • 95
  • Answer is right there! http://stackoverflow.com/a/27312494/1680919 – Wale Oct 01 '18 at 22:54
  • Firstly, i want to know if there is some API which is doing it before i must implement it on my own – K.Os Oct 01 '18 at 22:56
  • Look at this link for several answer showing how to set up a `BroadcastReceiver` together with the `NetworkInfo` to notify you of any changes in connectivity state:: https://stackoverflow.com/questions/40713270/how-to-get-network-state-change-on-android – Barns Oct 01 '18 at 23:41

2 Answers2

0

I will probably go for this method right here!

public boolean isOnline() {
    try {
        int timeoutMs = 1500;
        Socket sock = new Socket();
        SocketAddress sockaddr = new InetSocketAddress("8.8.8.8", 53);

        sock.connect(sockaddr, timeoutMs);
        sock.close();

        return true;
    } catch (IOException e) { return false; }
}
Wale
  • 1,644
  • 15
  • 31
  • This is a method which can be invoked once - i want to react on that change only when i am connected to WiFi but there is no internet connection – K.Os Oct 01 '18 at 23:01
  • what's your point? You want to react to a Wifi you're connected to that has no internet access??? you need the code to check if you're connected in the first place? – Wale Oct 01 '18 at 23:03
  • because if you're connected to a wifi or mobile! then you can do the check at the time you prefer! and it simply will return if you have internet access or not! pls explain if I'm missing anything!. – Wale Oct 01 '18 at 23:05
  • The point is i don't want to check if the access point has the internet connection, so the second part of your sentence is not what i am looking for. I want to be able to receive the status when i am connected to wifi but the AP has just lost the internet connection and react to it (e.g show exclamation mark like on Android status bar) – K.Os Oct 01 '18 at 23:07
  • 1
    I want to believe there is an api to do that! – Wale Oct 01 '18 at 23:15
-1

Firstly you have to create BroadcastReceiver:

public class NetworkChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
    isInternetAvailable();
}
private boolean isInternetAvailable() {
    try {
        return (Runtime.getRuntime().exec("ping -c 1 google.com").waitFor() == 0);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return false;
}

}

then you have to register in Manifest :

<receiver android:name=".NetworkChangeReceiver">
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
    </receiver>

at the end you have to register in your main application:

public class MainApplication extends MultiDexApplication {
@Override
public void onCreate() {
NetworkChangeReceiver myReceiver = new NetworkChangeReceiver();
    registerReceiver(myReceiver, filter);
}
}

so now any time you connect to WIFI you are going to receive a broadcast, and from isInternetAvailable() you will know if you are connecting to internet or not.

don't forget permissions:

<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" />
Zarea
  • 1