我像这样开始工作:
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.");
}
在服务中:
@Override
public boolean onStartJob(JobParameters jobParameters) {
Log.d("Service", "v1.2 Started");
我得到的输出是“服务正在运行”,而不是“服务已启动”。并且该应用未收到该服务应发送的广播。
这一切都可以在Android 8中完美运行,而在7中则不能。
编辑:我猜该服务必须正在运行,因为当我关闭并重新启动应用程序时,会收到消息“服务已安排好”。那么,为什么不能看到我的服务输出的日志?在牛轧糖中,它是否像在奥利奥中一样直接运行?还是我必须等待一个小时才能开始?
答案 0 :(得分:2)
请尝试以下操作:
// 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>
请下载this示例,然后从此处导入NotificationScheduler新项目并进行检查。请相应地更改您的项目。
我注意到的是
清单条目为:
android:name=".mService"
android:permission="android.permission.BIND_JOB_SERVICE"
></service>
工作计划程序:
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);
请注意,这是对周期性最小间隔的限制,不幸的是它是15分钟,当我们将其设置为较小时,SDK会将其更改为15分钟,同时将flex间隔设置为5分钟,最后我们得到该服务会在安排作业约7-8分钟后启动。就是这样)。
这对我有用,
祝你好运