问题
I have a service
that extends JobService
as below:
public class MyService extends JobService {
private int notificationNumber = 0;
private Thread thread;
@Override
public boolean onStartJob(JobParameters params) {
Toast.makeText(this, "Service1 Started with JobId: " + params.getJobId(), Toast.LENGTH_LONG).show();
thread = new WorkerThread(this, params);
thread.start();
return true;
}
@Override
public boolean onStopJob(JobParameters params) {
createNotification("Service1 onStop with JobId");
return false;
}
public void createNotification(String msg) {
notificationNumber++;
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentText(msg);
builder.setContentTitle("New Notification #" + notificationNumber);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setAutoCancel(true);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = builder.build();
manager.notify(notificationNumber, notification);
}
}
class WorkerThread extends Thread {
private MyService service;
private JobParameters params;
public WorkerThread(MyService service, JobParameters params) {
this.service = service;
this.params = params;
}
@Override
public void run() {
try {
service.createNotification("Service1.WorkerThread Started");
Thread.sleep(10000);
service.createNotification("Service1.WorkerThread Finished");
} catch (Exception e) {
e.printStackTrace();
} finally {
Log.i("##########", "Job Finished");
service.jobFinished(params, false);
}
}
}
and using JobInfo.builder
I instantiate from that and get it run by JobScheduler
as below:
public void scheduleService() {
int JOB_ID =1;
long y=100;
ComponentName serviceName = new ComponentName(this, MyService.class);
JobInfo jobInfo = new JobInfo.Builder(JOB_ID, serviceName)
// .setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
// .setBackoffCriteria(y,JobInfo.BACKOFF_POLICY_LINEAR)
.setPeriodic(y)
.setRequiresDeviceIdle(true)
.build();
JobScheduler scheduler = (JobScheduler) this.getSystemService(Context.JOB_SCHEDULER_SERVICE);
int result = scheduler.schedule(jobInfo);
if (result == JobScheduler.RESULT_SUCCESS) {
Log.d("Test", "Job scheduled successfully!");
}
}
even when I use:
JobInfo jobInfo = new JobInfo.Builder(JOB_ID, serviceName)
// .setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
// .setBackoffCriteria(y,JobInfo.BACKOFF_POLICY_LINEAR)
.setPeriodic(y)
// .setRequiresDeviceIdle(true)
.build();
nothing works. Although this:
if (result == JobScheduler.RESULT_SUCCESS) {
Log.d("Test", "Job scheduled successfully!");
}
shows that job is scheduled correctly but even 1 line of the service won't get called! Any Idea?
And about the manifest:
<service
android:name=".MyService"
android:enabled="true"
android:permission="android.permission.BIND_JOB_SERVICE"
android:exported="true"></service>
Everything seems right!!!
===========Edit without Idle requirement is gets fired but after a long time.
TG.
回答1:
Finally I found the reason. Everything was ok but the minimum time.
I found my question's answer at here:
Job Scheduler not running on Android N
As you see here the minimum time to set for scheduling is about 15 minutes. I left the phone 15 mins in the Idle mode and it got fired.
The corresponding log that I wasn't caring it was this:
01-19 22:39:01.775 31125-31125/ir.cclever.prepareforclass W/JobInfo: Specified interval for 114 is +10ms. Clamped to +15m0s0ms
I wish this post helps others encountering this issue.
TG.
来源:https://stackoverflow.com/questions/41749740/android-jobscheduler-with-idle-requirement-does-not-get-fired