Search code examples
android-thingsandroid-jobschedulerjobservice

JobService not running on Android Things


I try to work with JobScheduler and JobService on Android Things My installation is RPI (Raspberry Pi) running IoT RPI3 1.0.2

this is my simple code:

    package com.mystuff.jobservicetest;

    import android.app.Activity;
    import android.app.job.JobInfo;
    import android.app.job.JobParameters;
    import android.app.job.JobScheduler;
    import android.app.job.JobService;
    import android.content.ComponentName;
    import android.content.Context;
    import android.os.Bundle;
    import android.util.Log;


    public class MainActivity extends Activity {

    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        JobScheduler jobScheduler = (JobScheduler) getSystemService(
                Context.JOB_SCHEDULER_SERVICE);
        ComponentName name = new ComponentName(this, JobServiceTest.class);
        JobInfo jobInfo = new JobInfo.Builder(1,name).setPeriodic(1000).build();
        int result = jobScheduler.schedule(jobInfo);
        Log.d(TAG, "result = "+result);
    }

    public class JobServiceTest extends JobService {

        @Override
        public boolean onStartJob(JobParameters jobParameters) {
            Log.d(TAG, "Service job started");
            return false;
        }

        @Override
        public boolean onStopJob(JobParameters jobParameters) {
            return false;
        }
    }
 }

AndroidManifest.xml looks simple like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mystuff.jobservicetest">

    <application>
        <uses-library android:name="com.google.android.things" />

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.IOT_LAUNCHER" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <service android:name=".MainActivity$JobServiceTest"
            android:permission="android.permission.BIND_JOB_SERVICE"/>
    </application>

</manifest>

Outcome of logcat is just:

W/JobInfo: Specified interval for 1 is +1s0ms. Clamped to +15m0s0ms Specified flex for 1 is +1s0ms. Clamped to +5m0s0ms

D/MainActivity: result = 1

I am missing "Service job started" log. Seems to onStartJob is never called.

Any hints?

Thanks


Solution

  • A JobService can only be scheduled in intervals of 15 minutes or longer. If you want a shorter task, you may want to look at alternative methods.