0

I am developing an app that has following requirements 1)It should check the internet connectivity continuously in the background 2)when connectivity is found,it should send the email automatically 3) if the connectivity is not found,it should wait and keep checking the connectivity after every 10 or 15 sec. i have searched through and through but cannot find the connectivity check issue anywhere. I am new in android development. Please help me resolve this issue. Thank You.

Pradeep Simha
  • 17,683
  • 18
  • 56
  • 107
AB1
  • 17
  • 2

4 Answers4

0

Set alarm manager for your app which checks internet connection after every 15 seconds. Alarm manger broadcast an intent where you can do your stuff.

0

you can use a broadcast receiver that will check for the internet connectivity and whenever any change in the network it will be called.

public class ConnectivityReceiver extends BroadcastReceiver {
private static boolean firstConnect = true;

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


    final ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();

    if (activeNetInfo != null) {
        boolean isConnected = activeNetInfo.isConnected();
        boolean isWiFi = activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI;
        boolean isMobile = activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE;

        if (firstConnect) {

            firstConnect = false;

            //checks for the type of network, device is connected to.
            if (isConnected && isWiFi) {
                if(!Utilities.isReleaseMode(context)){
                    Toast.makeText(context, "Connected to Internet", Toast.LENGTH_SHORT).show();
                }
                Log.i("Wifi Network connected", "wifi Network connected");

            } else if (isConnected && isMobile) {
                if(!Utilities.isReleaseMode(context)) {
                    Toast.makeText(context, "Connected to Internet", Toast.LENGTH_SHORT).show();
                }
                Log.i("Mobile Network ", "mobile Network connected");

            }
        }

    } else {
        firstConnect = true;
        //  Log.i("Network Disconnected", "Network Disconnected");
    }
}
Mohit
  • 505
  • 8
  • 31
  • A change in the network might not be related to internet connectivity restoration, it is always better to reach a real server to check connectivity. – Madusudanan Dec 17 '15 at 06:50
  • Performance wise i would prefer to have a broadcast receiver, rather then calling real server periodically. – Mohit Dec 17 '15 at 06:52
  • I would prefer a backoff mechanism as I mentioned, where the wait time is increased with each failure attempt to ping, that is the way almost many apps are implemented – Madusudanan Dec 17 '15 at 06:56
0
  Check internet connectivity:
   public static boolean isNetworkStatusAvialable (Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager)   context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivityManager != null) 
    {
        NetworkInfo netInfos = connectivityManager.getActiveNetworkInfo();
        if(netInfos != null)
            if(netInfos.isConnected()) 
                if (netInfos.isAvailable())
                    return true;
    }
    return false;

}

 if (isNetworkStatusAvialable(getApplicationContext()))
    {
   // your function
 }else{}
MurugananthamS
  • 2,395
  • 4
  • 20
  • 49
  • but this will just check the internet connectivity. i want to add further details to it that it should continuously keep checking for internet. and if the connection is found, it should fetch my file from the internal storage and send it automatically. – AB1 Dec 17 '15 at 06:55
  • call this function into Asynchronous task ,it will run backround Automatically @AvantiBhandarkar – MurugananthamS Dec 17 '15 at 06:59
0

Call this function every 10 or 15 sec using alaram.

public static boolean isOnline(Context context) {// requires network state
                                                    // access permisstion
    ConnectivityManager cm = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    return netInfo != null && netInfo.isConnectedOrConnecting();
}

 final Handler handler = new Handler();
    Timer timer = new Timer();
    TimerTask doAsynchronousTask = new TimerTask() {
        @Override
        public void run() {
            handler.post(new Runnable() {
                @SuppressWarnings("unchecked")
                public void run() {
                    try {
                        isOnline(getActivity());
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                    }
                }
            });
        }
    };
    timer.schedule(doAsynchronousTask, 0, 10000);
RDY
  • 613
  • 1
  • 9
  • 25