I attempted to use a Service to solve my problem but as HeyAlex pointed out, Services work differently in Oreo v8.0+ of Android and requires you to have a notification appear which is very unsightly as far as UI is concerned.
After doing some further research I discovered a separate Stackoverflow thread regarding these changes:
Android 8.0: java.lang.IllegalStateException: Not allowed to start service Intent
This might not be the best solution as it's deprecated and no longer maintained but, JobServiceIntent works with Oreo and is backwards compatible. You will need the following permissions in the manifest:
<!-- < 8.0 -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- >= 8.0, inside application -->
<service
android:name="com.example.ListenerService"
android:permission="android.permission.BIND_JOB_SERVICE" />
I then have my service extending JobIntentService instead of Service and it all works; the network listener is setup without the need of any activity and no notification on Android 8+.
public class ListenerService extends JobIntentService {
public static final int JOB_ID = 1;
@Override
protected void onHandleWork(@NonNull Intent intent) {
getApplicationContext().registerReceiver(networkChangeReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
public static void enqueueWork(Context context, Intent intent) {
ListenerService.context = context;
enqueueWork(context, ListenerService.class, JOB_ID, intent);
}
private static final BroadcastReceiver networkChangeReceiver = new BroadcastReceiver() {
// usual broadcast methods in here such as onReceive
}
}
The service can be triggered using:
ListenerService.enqueueWork(context,intent);