Manish
Manish

Reputation: 157

How to check internet connectivity continuously in background while android application running

I want to check internet connectivity continuously in background while my android application running. I mean from starting of the application to the end of the application. How can i do it? What should be the approach?

Upvotes: 12

Views: 38175

Answers (7)

Meet
Meet

Reputation: 950

ConnectivityObserver.kt:

package com.example.checkInternet

import kotlinx.coroutines.flow.Flow

interface ConnectivityObserver {

    fun observe(): Flow<Status>

    enum class Status {
        Available, Unavailable, Losing, Lost
    }
}

NetworkConnectivityObserver.kt:

package com.example.checkInternet

import android.content.Context
import android.net.*
import android.os.Build
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.launch


@OptIn(ExperimentalCoroutinesApi::class)
class NetworkConnectivityObserver(
    context: Context,
): ConnectivityObserver {

    private val connectivityManager =
        context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
    private val networkRequest = NetworkRequest.Builder()
        .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
        .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
        .build()

    override fun observe(): Flow<ConnectivityObserver.Status> {
        return callbackFlow {
            val callback = object : ConnectivityManager.NetworkCallback() {
                override fun onAvailable(network: Network) {
                    super.onAvailable(network)
                    launch { send(ConnectivityObserver.Status.Available) }
                }

                override fun onLosing(network: Network, maxMsToLive: Int) {
                    super.onLosing(network, maxMsToLive)
                    launch { send(ConnectivityObserver.Status.Losing) }
                }

                override fun onLost(network: Network) {
                    super.onLost(network)
                    launch { send(ConnectivityObserver.Status.Lost) }
                }

                override fun onUnavailable() {
                    super.onUnavailable()
                    launch { send(ConnectivityObserver.Status.Unavailable) }
                }
            }

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                connectivityManager.registerDefaultNetworkCallback(callback)
            }else{
                connectivityManager.registerNetworkCallback(networkRequest, callback)
            }
            awaitClose {
                connectivityManager.unregisterNetworkCallback(callback)
            }
        }.distinctUntilChanged()
    }
}

MainActivity.kt

package com.example.checkInternet

import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.example.checkInternet.ConnectivityObserver
import com.example.checkInternet.NetworkConnectivityObserver
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity() {

    private val connectivityObserver : NetworkConnectivityObserver by lazy {
        NetworkConnectivityObserver(this)
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView( R.layout.activity_main)

        lifecycleScope.launch {
            connectivityObserver.observe().collect {
                when (it) {
                    ConnectivityObserver.Status.Available -> {
                      Toast.makeText(this@MainActivity,"Network Available",Toast.LENGTH_LONG).show()
                    }
                    ConnectivityObserver.Status.Unavailable -> {
                        Toast.makeText(this@MainActivity,"Network Unavailable",Toast.LENGTH_LONG).show()
                    }
                    ConnectivityObserver.Status.Losing -> {
                        Toast.makeText(this@MainActivity,"Network Losing",Toast.LENGTH_LONG).show()
                    }
                    ConnectivityObserver.Status.Lost -> {
                        Toast.makeText(this@MainActivity,"Network Lost",Toast.LENGTH_LONG).show()

                    }
                }
            }
        }

    }

}


Upvotes: 2

Akhilesh Chaudhary
Akhilesh Chaudhary

Reputation: 43

In Activity or fragment

public static final String BROADCAST = "checkinternet";
IntentFilter intentFilter;

inside oncreate

   intentFilter = new IntentFilter();
   intentFilter.addAction(BROADCAST);
   Intent serviceIntent = new Intent(this,Networkservice.class);
   startService(serviceIntent);
   if (Networkservice.isOnline(getApplicationContext())){
       Toast.makeText(getApplicationContext(),"true",Toast.LENGTH_SHORT).show();
   }else
       Toast.makeText(getApplicationContext(),"false",Toast.LENGTH_SHORT).show();

outside oncreate

 public BroadcastReceiver broadcastReceiver=new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(BROADCAST)){
            if (intent.getStringExtra("online_status").equals("true")){
                Toast.makeText(getApplicationContext(),"true",Toast.LENGTH_SHORT).show();
                Log.d("data","true");
            }else {
                Toast.makeText(getApplicationContext(), "false", Toast.LENGTH_SHORT).show();
                Log.d("data", "false");
            }
        }
    }
};

@Override
protected void onRestart() {
    super.onRestart();
    registerReceiver(broadcastReceiver,intentFilter);
}

@Override
protected void onPause() {
    super.onPause();
    unregisterReceiver(broadcastReceiver);
}

@Override
protected void onResume() {
    super.onResume();
    registerReceiver(broadcastReceiver,intentFilter);
}

//class

public class Networkservice extends Service {

@Override
public void onCreate() {
    super.onCreate();
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    handler.post(perioud);
    return START_STICKY;
}
public static boolean isOnline(Context context){
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo nf=cm.getActiveNetworkInfo();
    if (nf!=null&&nf.isConnectedOrConnecting())
        return true;
        else
            return false;
}
Handler handler=new Handler();
private Runnable perioud =new Runnable() {
    @Override
    public void run() {
        handler.postDelayed(perioud,1*1000- SystemClock.elapsedRealtime()%1000);

        Intent intent = new Intent();
        intent.setAction(MainActivity.BROADCAST);
        intent.putExtra("online_status",""+isOnline(Networkservice.this));
        sendBroadcast(intent);
    }
};
}

Upvotes: 1

Ajay Venugopal
Ajay Venugopal

Reputation: 1710

Create class that extends BroadcastReceiver, use ConnectivityManager.EXTRA_NO_CONNECTIVITY to get connection info.

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

public class CheckConnectivity extends BroadcastReceiver{

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

    boolean isConnected = arg1.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
    if(isConnected){
        Toast.makeText(context, "Internet Connection Lost", Toast.LENGTH_LONG).show();
    }
    else{
        Toast.makeText(context, "Internet Connected", Toast.LENGTH_LONG).show();
    }
   }
 }

Add this permission in mainfest too

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  <uses-permission android:name="android.permission.INTERNET" />

Upvotes: 3

Ganesh
Ganesh

Reputation: 943

To check internet connectivity simply register the broadcast for CONNECTIVITY_CHANGE and WIFI_STATE_CHANGED. So whenever internet is connected or disconnected event occurs onReceive method of our BroadcastReceiver gets called where we can write our logic.

In AndroidManifest.xml

 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
     <uses-permission android:name="android.permission.INTERNET" />
     <receiver
            android: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>

In NetworkChangeReceiver class.

 public class NetworkChangeReceiver extends BroadcastReceiver {
    
        @Override
        public void onReceive(final Context context, final Intent intent) {
    
            int status = NetworkUtil.getConnectivityStatusString(context);
            if ("android.net.conn.CONNECTIVITY_CHANGE".equals(intent.getAction())) {
                if (status == NetworkUtil.NETWORK_STATUS_NOT_CONNECTED) {
                 
                  //write code for internet is disconnected...

                } else {

                   //write code for internet is connected...

                }
           }
        }
    }

To check the status of internet.

 public class NetworkUtil {
    public static final int TYPE_NOT_CONNECTED = 0;
        public static final int TYPE_WIFI = 1;
        public static final int TYPE_MOBILE = 2;
        public static final int NETWORK_STATUS_NOT_CONNECTED = 3;
        public static final int NETWORK_STATUS_WIFI = 4;
        public static final int NETWORK_STATUS_MOBILE = 5;
    
        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 int getConnectivityStatusString(Context context) {
            int conn = NetworkUtil.getConnectivityStatus(context);
            int status = 0;
            if (conn == NetworkUtil.TYPE_WIFI) {
                status = NETWORK_STATUS_WIFI;
            } else if (conn == NetworkUtil.TYPE_MOBILE) {
                status = NETWORK_STATUS_MOBILE;
            } else if (conn == NetworkUtil.TYPE_NOT_CONNECTED) {
                status = NETWORK_STATUS_NOT_CONNECTED;
            }
            return status;
        }
    }

Upvotes: 0

vasurb
vasurb

Reputation: 129

I know I am late to answer this Question. But here is a way to monitor the Network Connection Continuously in an Activity/Fragment.

We will use Network Callback to monitor our connection in an Activity/Fragment

first, declare two variable in class

// to check if we are connected to Network
boolean isConnected = true;

// to check if we are monitoring Network
private boolean monitoringConnectivity = false;

Next, We will write the Network Callback Method

private ConnectivityManager.NetworkCallback connectivityCallback
            = new ConnectivityManager.NetworkCallback() {
        @Override
        public void onAvailable(Network network) {
            isConnected = true;
            LogUtility.LOGD(TAG, "INTERNET CONNECTED");
        }

        @Override
        public void onLost(Network network) {
            isConnected = false;
            LogUtility.LOGD(TAG, "INTERNET LOST");
        }
    };

Now Since we have written our Callback we will Now write a method which will monitor our Network Connection, and in this method, we will register our NetworkCallback.

// Method to check network connectivity in Main Activity
    private void checkConnectivity() {
        // here we are getting the connectivity service from connectivity manager
        final ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(
                Context.CONNECTIVITY_SERVICE);

        // Getting network Info
        // give Network Access Permission in Manifest
        final NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();

        // isConnected is a boolean variable
        // here we check if network is connected or is getting connected
        isConnected = activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting();

        if (!isConnected) {
            // SHOW ANY ACTION YOU WANT TO SHOW
            // WHEN WE ARE NOT CONNECTED TO INTERNET/NETWORK
            LogUtility.LOGD(TAG, " NO NETWORK!");
// if Network is not connected we will register a network callback to  monitor network
            connectivityManager.registerNetworkCallback(
                    new NetworkRequest.Builder()
                            .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
                            .build(), connectivityCallback);
            monitoringConnectivity = true;
        }

    }

All our work is almost finished we just need to call our checkConnectivity(), the method in onResume() of our Activity/Fragment

  @Override
    protected void onResume() {
        super.onResume();
        checkConnectivity();
    }

*unRegister the NetworkCallback in onPause() ,method of your Activity/Fragment *

   @Override
    protected void onPause() {
        // if network is being moniterd then we will unregister the network callback
        if (monitoringConnectivity) {
            final ConnectivityManager connectivityManager
                    = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            connectivityManager.unregisterNetworkCallback(connectivityCallback);
            monitoringConnectivity = false;
        }
        super.onPause();
    }

That's it, just add this code in which every class of your Activity/Fragment were you need to monitor the Network!.And don't forget to unregister it!!!

Upvotes: 6

Oscar J. Irun
Oscar J. Irun

Reputation: 475

You can also try this service Class. Just set the time interval in seconds and the url to ping:

Android Check Internet Connection

Just remember to add the service to the Manifest file, and add the permissions

Upvotes: 0

lucian.pantelimon
lucian.pantelimon

Reputation: 3669

The Android system already does that for you. Instead of constantly polling the phone for the network state, register BroadcastReceivers for WiFi and network state changes and process the Intents sent by the system when the WiFi or network state changes.

Have a look at this question for more information on this.

Once you register your BroadcastReceiver, the network state change listening will happen in the background (in the OS) and an Intent will be sent to your receivers on the UI thread (most likely) when the network state changes. If you want to receive the Intents on a background thread, check this question for more info.

Upvotes: 0

Related Questions