Android-Network-Connectivity icon indicating copy to clipboard operation
Android-Network-Connectivity copied to clipboard

Detect CONNECTIVITY CHANGE in Android 7 and above whether app is in foreground or in background

Android-Network-Connectivity

Detect CONNECTIVITY CHANGE in Android 7 and above whether app is in foreground or in background

Pre-requisites

  • Android SDK 27
  • Android Build Tools v27.0.2
  • Android Support Repository

Getting Started

This sample uses the Gradle build system. To build this project, use the "gradlew build" command or use "Import Project" in Android Studio.

Schedule job

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void scheduleJob() {
        JobInfo myJob = new JobInfo.Builder(0, new ComponentName(this, NetworkSchedulerService.class))
                .setRequiresCharging(true)
                .setMinimumLatency(1000) 
                .setOverrideDeadline(2000) // set the max delay for receiving call
                .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
                .setPersisted(true)
                .build();

        JobScheduler jobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
        jobScheduler.schedule(myJob);
    }