S.M_Emamian
S.M_Emamian

Reputation: 17393

Checking internet connection with service on android

I know how to check for internet connectivity when my app is open using activity. But how to check for connectivity in service when my app is not running?

Upvotes: 7

Views: 14917

Answers (3)

Adnan
Adnan

Reputation: 5075

You might need to use broadcast receiver. You will continuously receive updates in connectivity.(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 service class:

class YourService extends IntentService{

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

   }

}

Upvotes: 13

panini
panini

Reputation: 2026

The system provides a Broadcast when the network connectivity changes, which you can read using a BroadcastReceiver. This will be called whether your app is open or closed.

Upvotes: 3

Sanket Patel
Sanket Patel

Reputation: 1160

Using below code you can test whether device is connected with internet or not. You can use this function anytime when you try to call any webservice or any task related to internet. You can use this function in android service which runs in 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;
    }

Upvotes: 0

Related Questions