Chud37
Chud37

Reputation: 5027

Nougat - JobScheduler RESULT_SUCCESS but no output from job

I start a job like so:

JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);

    if(scheduler.getPendingJob(JOB_NUMBER) == null) {

        ComponentName componentName = new ComponentName(this, mService.class);
        JobInfo info = new JobInfo.Builder(JOB_NUMBER, componentName)
                .setRequiresCharging(false)
                .setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
                .setPersisted(true)
                .setPeriodic(60 * 60 * 1000)
                .build();

        int resultCode = scheduler.schedule(info);
        if (resultCode == JobScheduler.RESULT_SUCCESS) {
            Log.d(TAG, "Service is not running, Job " + String.valueOf(JOB_NUMBER) + " Scheduled.");
        } else {
            Log.d(TAG, "Service is not running, However job scheduling failed.");
        }

    } else{
        Log.d(TAG, "Service is already scheduled.");
    }

And in the service:

@Override
public boolean onStartJob(JobParameters jobParameters) {

    Log.d("Service", "v1.2 Started");

I get the output 'Service is running' but not the 'Service Started'. And the app isn't receiving the broadcasts the service should be sending either.

This all works perfectly in Android 8, but not in 7.

Edit: I guess the service must be running because when I close and restart the app I get the message 'Service is already scheduled'. So why then, can I not see any log output from my service? In Nougat, does it run straight away as in Oreo? Or do I have to wait an hour before it starts?

Upvotes: 1

Views: 580

Answers (1)

Hovanes Mosoyan
Hovanes Mosoyan

Reputation: 759

Please try this:

// the service class

import android.app.job.JobParameters;
import android.app.job.JobService;
import android.util.Log;

/**
 * JobService to be scheduled by the JobScheduler.
 * start another service
 */
public class mService extends JobService {
    private static final String TAG = "SyncService";

    @Override
    public boolean onStartJob(JobParameters params) {
        Log.d("TAG", "onStartJobb");
        return true;
    }

    @Override
    public boolean onStopJob(JobParameters params) {
        Log.d("TAG", "onStopJob");
        return true;
    }
}


// in main activity

@RequiresApi(api = Build.VERSION_CODES.N)
public void onResume(){
    super.onResume();

    JobScheduler scheduler = getSystemService(JobScheduler.class);

    if(scheduler.getPendingJob(1) == null) {

        ComponentName componentName = new ComponentName(this, mService.class);
        //JobInfo.Builder builder = new JobInfo.Builder(0, serviceComponent);
        JobInfo.Builder info = new JobInfo.Builder(1, componentName)
                .setRequiresCharging(false)
                .setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
                .setPersisted(true)
                .setMinimumLatency(1 * 1000)
                .setOverrideDeadline(3 * 1000)
                //.setPeriodic(60 * 60 * 1000)
                //.build()
                ;

        int resultCode = scheduler.schedule(info.build());
        if (resultCode == JobScheduler.RESULT_SUCCESS) {
            Log.d("TAG", "Service is not running, Job " + String.valueOf(1) + " Scheduled.");
        } else {
            Log.d("TAG", "Service is not running, However job scheduling failed.");
        }

    } else{
        Log.d("TAG", "Service is already scheduled.");
    }
}

    // and in manifest

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <service
        android:name=".mService"
        android:enabled="true"
        android:exported="true"
        android:label="Word service"
        android:permission="android.permission.BIND_JOB_SERVICE">
    </service>

Please download this samples and import the NotificationScheduler new project from there and check it. Please change your project accordingly.

What I noticed:

  1. the manifest entry is:

        android:name=".mService"
        android:permission="android.permission.BIND_JOB_SERVICE"
        ></service>
    
  2. the job scheduler:

    ComponentName serviceName = new ComponentName(getPackageName(), mService.class.getName());
    
    long min = JobInfo.getMinPeriodMillis();
    JobInfo.Builder builder = new JobInfo.Builder(JOB_ID, serviceName)
            .setRequiredNetworkType(NETWORK_TYPE_NONE)
            .setRequiresDeviceIdle(false)
            .setPeriodic(5000)
            .setRequiresCharging(false);
    builder.setPeriodic(5 * 1000);
    
    JobInfo myJobInfo = builder.build();
    JobScheduler jobScheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
    jobScheduler.schedule(myJobInfo);
    

Please note that here is a restriction for Periodic min interval, unfortunately it is 15 minutes, and when we set it smaller that, the SDK changes it to 15 minutes, and also it sets the flex interval 5 minutes, finally we get the service start after about 7-8 minutes from scheduling time of the Job. That is it ).

this works for me,

Good luck )

Upvotes: 2

Related Questions