Android Internet Connection checker OUTSIDE the app
<p>My app needs to do something whenever the internet is connected and vice-versa. However, I need to do this <strong>OUTSIDE</strong> my application. There are a lot of ideas and help here on stackoverflow but it is always <strong>WITHIN</strong> the application. I know that the AlarmManager works outside the application but it would just be draining the battery if I try to check if there's internet connection every 5 minutes. What I wanted is to check the connection outside the app to download something. </p>

<p>I also found out that Intent Service can work outside the application as well. I have tried to put this inside the Wakeful Broadcast Receiver. However, it is still not being called.</p>

<p>Can someone out there help me? Thanks! </p>

<p>Here's my Wakeful Broadcast Receiver </p>

<pre><code>public class ConnectivityOutsideAppReceiver extends WakefulBroadcastReceiver {

    private static final String LOG_TAG = ConnectivityOutsideAppReceiver.class.getSimpleName();

    private ConnectivityManager connectivityManager;
    private static boolean connection = false;

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

        ComponentName comp = new ComponentName(context.getPackageName(),
                ConnectivityOutsideAppService.class.getName());

        // Start the service, keeping the device awake while it is
        // launching.
        startWakefulService(context, (intent.setComponent(comp)

        ));
    }

}
</code></pre>

<p>This is the Intent Service</p>

<p>public class ConnectivityOutsideAppService extends IntentService {</p>

<pre><code>private static final String LOG_TAG = ConnectivityOutsideAppService.class.getSimpleName();
private ConnectivityManager connectivityManager;
private static boolean connection = false;
private Context context;

public ConnectivityOutsideAppService() {
   super(ConnectivityOutsideAppService.class.getSimpleName());
    this.context = context;
}

@Override
protected void onHandleIntent(Intent intent) {
    connectivityManager =
            (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    checkConnectionOnDemand();

    Log.e(LOG_TAG, " ## onHandleIntent##");
    if (connection == true
            &amp;&amp; intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,
            false)) {
        Log.e(LOG_TAG, " ## connection == true ##");
        connection = false;
    } else if (connection == false
            &amp;&amp; !intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,
            false)) {
        Log.e(LOG_TAG, " ## connection == false ##");
        connection = true;
    }

    ConnectivityOutsideAppReceiver.completeWakefulIntent(intent);
}

public static boolean hasConnection() {
    return connection;
}

private void checkConnectionOnDemand() {
    Log.e(LOG_TAG, " ## checkConnectionOnDemand ##");
    final NetworkInfo info = connectivityManager.getActiveNetworkInfo();
    if (info == null || info.getState() != NetworkInfo.State.CONNECTED) {
        if (connection == true) {
            Log.e(LOG_TAG, " ## connection == true ##");
            connection = false;
        }
    } else {
        if (connection == false) {
            Log.e(LOG_TAG, " ## connection == false ##");
            connection = true;
        }
    }
}
}
</code></pre>

<p>This is my Android Manifest </p>

<pre><code>    &lt;uses-permission android:name="android.permission.INTERNET" /&gt;

    &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;

    &lt;service
        android:name="com.timetrackermobilelog.BusinessServices.ConnectivityOutsideAppService"
        android:exported="false"/&gt;

    &lt;receiver android:name="com.timetrackermobilelog.Utilities.ConnectivityOutsideAppService"
              android:enabled="true"
              android:process=":remote"&gt;

        &lt;intent-filter android:priority="1000" &gt;
            &lt;action android:name="com.timetrackermobilelog.Utilities.ConnectivityOutsideAppService" /&gt;
            &lt;category android:name="com.Utilities" /&gt;
        &lt;/intent-filter&gt;
    &lt;/receiver&gt;
</code></pre>

<p>I have tried registering the receiver inside the Activity but it doesn't work as well. </p>

<pre><code>IntentFilter intentFilter = new IntentFilter("com.pointwest.timetrackermobilelog.Utilities.ConnectivityOutsideAppReceiver");
ConnectivityOutsideAppReceiver connectivityOutsideAppReceiver = new ConnectivityOutsideAppReceiver();
registerReceiver(connectivityOutsideAppReceiver, intentFilter);
</code></pre>

<p>Is there anything that I missed?</p>