title: On Wifi/Mobile data Open i want to run a service and Wifi/Mobile data closed i want to stop the service in android
body: <h2>Here is my service class,that i want to start on wifi / mobile data open.</h2>
  
  <hr>
  
  <pre><code>public class BackgroundService extends Service {
  @Override
  public void onCreate() {
      super.onCreate();
  }
  
  @Nullable
  @Override
  public IBinder onBind(Intent intent) {
      return null;
  }
  
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
      Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
      return START_STICKY;
  }
  
  @Override
  public void onDestroy() {
      super.onDestroy();
  }
  </code></pre>
  
  <p>}</p>
  
  <hr>
  
  <h2>Here is my BroadcastReceiver class that is detecting the status of wifi/mobile data open / closed..</h2>
  
  <pre><code>public class ConnectivityChangeReceiver 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();
  
          int conn = NetworkUtil.getConnectivityStatus(context);
          String statusa = null;
          if (conn == NetworkUtil.TYPE_WIFI) {
              statusa = "Wifi enabled";
              ComponentName comp = new ComponentName(context.getPackageName(),
                      BackgroundService.class.getName());
              context.startService((intent.setComponent(comp)));
          } else if (conn == NetworkUtil.TYPE_MOBILE) {
              statusa = "Mobile data enabled";
              ComponentName comp = new ComponentName(context.getPackageName(),
                      BackgroundService.class.getName());
              context.startService((intent.setComponent(comp)));
          } else if (conn == NetworkUtil.TYPE_NOT_CONNECTED) {
              statusa = "Not connected to Internet";
          }
  
      }
  </code></pre>
  
  <p>}</p>
  
  <h2>Here is Another class that is associated with Recever class</h2>
  
  <pre><code>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;
  }
  </code></pre>
  
  <p>}</p>
  
  <h1>I have mentioned service and BroadcastReceiver in AndroidManifest.xml file and also mention uses-permission..</h1>