我想用一个JobService替换意图过滤器“ android.intent.action.ACTION_POWER_CONNECTED”的AndroidManifest接收器,每次将其连接到设备时,该JobService都会调用一次。但是onStartJob()ist在jobScheduler.schedule(builder.build());上调用了一次设备ist接通电源后再也不会调用。预先感谢:
在清单中,添加了一项服务:
<service
android:name=".BatteryJobService"
android:label="Battery service"
android:permission="android.permission.BIND_JOB_SERVICE" >
</service>
用于调度jobService的代码:
ComponentName serviceComponent = new ComponentName(context, BatteryJobService.class);
JobInfo.Builder builder = new JobInfo.Builder(0, serviceComponent);
builder.setRequiresCharging(true);
JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
jobScheduler.schedule(builder.build());
和JobService:
public class BatteryJobService extends JobService {
@Override
public boolean onStartJob(JobParameters params) {
return false; // true also tested
}
@Override
public boolean onStopJob(JobParameters params) {
return true;
}
}