你们谁能告诉我为什么这个预定工作中的onStartJob()
从未被调用过?
我正在测试O-MR1。当服务绑定时,我看到来自onServiceConnected()
调用的日志记录,但我从未看到来自onStartJob()
的日志记录。
这是工作:
public class LogUploadJobService extends JobService {
private final String TAG = this.getClass().getSimpleName();
private BikeTrackerService service;
private JobParameters jobParameters;
ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder serviceBinder) {
// We've bound to LocalService, cast the IBinder and get LocalService instance.
BikeTrackerService.BikeTrackerServiceBinder binder = (BikeTrackerService.BikeTrackerServiceBinder)serviceBinder;
service = binder.getService();
Logg.d(TAG, "onServiceConnected() We're bound, BikeTrackerService is now available to the LogUploadJobService.");
// We can now start getting other callbacks.
onServiceBound();
}
@Override
public void onServiceDisconnected(ComponentName name) {
Logg.d(TAG, "onServiceDisconnected()");
}
};
@Override
public void onCreate() {
Logg.d(TAG, "onCreate() " + this.hashCode());
super.onCreate();
}
@Override
public boolean onStartJob(final JobParameters params) {
Logg.d(TAG, "onStartJob()");
jobParameters = params;
// Now bind to the service, whether or not it's already running. Calls back to onServiceBound() below. This should never
// create a new instance, despite using BIND_AUTO_CREATE.
Intent serviceIntent = new Intent(this, BikeTrackerService.class);
bindService(serviceIntent, connection, Context.BIND_AUTO_CREATE);
// Return true as there's more work to be done with this job.
return true;
}
@Override
public boolean onStopJob(JobParameters params) {
Logg.d(TAG, "onStopJob()");
// Return false to drop the job.
return false;
}
@Override
public void onDestroy() {
Logg.d(TAG, "onDestroy() " + this.hashCode());
super.onDestroy();
// Unbind from service if bound.
if (service == null) {
return;
}
Logg.d(TAG, "Unbinding from service.");
unbindService(connection);
}
private void onServiceBound() {
FileLogger fileLogger = FileLogger.getInstance();
String log = fileLogger.getLog();
// The service will now put this log into the Firebase database.
service.addAppLog(log);
// We're done and we don't need a reschedule.
jobFinished(jobParameters, false);
}
}
这是我如何安排它,从扩展应用程序的类:
private void rescheduleLogUploadJob() {
ComponentName logUploadJob = new ComponentName(this, LogUploadJobService.class);
// Reuse the same job ID every time, to replace any previously scheduled job.
JobInfo.Builder builder = new JobInfo.Builder(SCHEDULED_JOB_ID, logUploadJob);
// Only upload the logs when the device is charging and on an unmetered network. No need to wait for idle.
builder.setRequiresCharging(true);
builder.setRequiresDeviceIdle(false);
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED);
// For minSdkVersion 23:
// Upload, at most, every six hours, within the above constraints.
// builder.setPeriodic(INTERVAL_6_HOURS_IN_MILLIS);
// Upload, at most, every six hours and at worst, every 12 hours, within the above constraints.
builder.setPeriodic(INTERVAL_6_HOURS_IN_MILLIS, INTERVAL_6_HOURS_IN_MILLIS);
// Persist the schedule across device reboots.
builder.setPersisted(true);
// Schedule job.
JobScheduler scheduler = (JobScheduler)getSystemService(Context.JOB_SCHEDULER_SERVICE);
if (scheduler == null) {
Logg.e(TAG, "No scheduler. Can't schedule log upload job.");
return;
}
Logg.d(TAG, "(Re)scheduling log upload job");
scheduler.schedule(builder.build());
}
(不要介意Logg
类,顺便说一下。它只是android.util.Log
的包装。)
[编辑]
我也在日志中看到了这一点:
SchedPolicy: set_timerslack_ns write failed: Operation not permitted
答案 0 :(得分:0)
抱歉,我的错误。 onStartJob()
被调用,但我对logcat的过滤阻止了我看到它。
set_timerslack_ns write failed
是否有任何问题,我仍然不确定,所以会接受任何解决这个问题的答案。