Checking your internet connection with Android service

I know how to check my internet connection when my application is open using activity. But how to check the connection in the service when my application is not working?

+6
source share
4 answers

You may need to use a broadcast receiver. You will constantly receive updates on the connection. (Connected / Disconnected)

Example:

manifest:

Permissions:

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

Register Broadcast Receiver:

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

Create receiver class:

public class ConnectivityChangeReceiver extends BroadcastReceiver {


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

        // Explicitly specify that which service class will handle the intent.
        ComponentName comp = new ComponentName(context.getPackageName(),
                YourService.class.getName());
        intent.putExtra("isNetworkConnected",isConnected(context));
        startService(context, (intent.setComponent(comp)));
    }

 public  boolean isConnected(Context context) {
           ConnectivityManager connectivityManager = ((ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE));
           NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
           return networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected();
   }

}

Your class of service:

class YourService extends IntentService{

    @Override
    protected void onHandleIntent(Intent intent) {
      Bundle extras = intent.getExtras();
      boolean isNetworkConnected = extras.getBoolean("isNetworkConnected");
      // your code

   }

}
+11
source

Broadcast , BroadcastReceiver. , .

+2

.

public static boolean isConnected(Context context) {
   ConnectivityManager connectivityManager = ((ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE));
   NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
   return networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected();
}

. - .

0

Using the code below, you can check if the device is connected to the Internet or not. You can use this function at any time when trying to call any web service or any task related to the Internet. You can use this feature in an Android service that runs in the background.

public boolean isConnectingToInternet(Context _context){
        ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
          if (connectivity != null)
          {
              NetworkInfo[] info = connectivity.getAllNetworkInfo();
              if (info != null)
                  for (int i = 0; i < info.length; i++)
                      if (info[i].getState() == NetworkInfo.State.CONNECTED)
                      {
                          return true;
                      }

          }
          return false;
    }
0
source

Source: https://habr.com/ru/post/1531044/


All Articles