user6825303
user6825303

Reputation: 21

Android - Need help in getting a trigger when WiFi is on and internet is lost

I am a new bee in Android. I am trying to find if there is any way of getting notified (like broadcast receiver for connection status changed) when WiFi is in connected state and Internet connection is lost.

I tried using CONNECTION_CHANGED in manifest file for my receiver. It could not identify if internet connection is lost though WiFi is connected.

I need it to happen in background so that I can login to our service providers page to access internet.

Please help me on how I can get it done.

EDIT: My receiver (registered in manifest for connection changed) is not getting called when internet connection is lost though WiFi is still in connected state. I don't want to start an activity. Instead, I would like to run in background (Asynchtask) when I get this notification.

Thanks in advance.

Upvotes: 2

Views: 994

Answers (2)

Mann
Mann

Reputation: 560

public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager connectivityManager
                = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();

        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }

This will return

true if wi-fi/mobile data is connected and the internet connection is active.

false if wi-fi/mobile data is connected but no internet access.

false if wi-fi & mobile data isn't connected at all.

In order to get notified when Internet connection gets lost, One approach I can suggest is to start a service that runs isNetworkAvailable() method I wrote above. You can run it every second(or 5 seconds would be ok) and when it returns false call a custom BroadcastReceiver and on 'onReceive' of that BroadcastReceiver you can build a Notification. Make sure that you create the notification on the first return of 'false' after 'true(s)

This is maybe not the best way but this won't slow down your app I suppose. Prefer to call it every 5 seconds or more.

Upvotes: 1

W4R10CK
W4R10CK

Reputation: 5550

Create a class for network status:

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class NetworkUtil {

public static int TYPE_WIFI = 1;
public static int TYPE_MOBILE = 2;
public static int TYPE_NOT_CONNECTED = 0;


public static int getConnectivityStatus(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (null != activeNetwork) {
        if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
            return TYPE_WIFI;

        if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
            return TYPE_MOBILE;
    } 
    return TYPE_NOT_CONNECTED;
}

public static String getConnectivityStatusString(Context context) {
    int conn = NetworkUtil.getConnectivityStatus(context);
    String status = null;
    if (conn == NetworkUtil.TYPE_WIFI) {
        status = "Wifi enabled";
    } else if (conn == NetworkUtil.TYPE_MOBILE) {
        status = "Mobile data enabled";
    } else if (conn == NetworkUtil.TYPE_NOT_CONNECTED) {
        status = "Not connected to Internet";
    }
    return status;
}
}

Broadcast receiver:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class NetworkChangeReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(final Context context, final Intent intent) {

        String status = NetworkUtil.getConnectivityStatusString(context);

        Toast.makeText(context, status, Toast.LENGTH_LONG).show();
    }
}

Menifest file:

<application  ...>
 ...
    <receiver
        android:name="your_package_name.NetworkChangeReceiver"
        android:label="NetworkChangeReceiver" >
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
        </intent-filter>
    </receiver>
  ...

Upvotes: 1

Related Questions