我是jobscheduler api的新手。我的要求是在设备插入充电时启动工作服务。这是以下配置
JobScheduler jobScheduler =
(JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
jobScheduler.schedule(new JobInfo.Builder(1,
new ComponentName(this, MyService.class))
.setRequiresCharging(true)
.setPeriodic(3000)
.setPersisted(true)
.build());
这不适合我。它仅适用于setPeriodic事件。我试过插件并插入移动设备。任何人都可以帮忙吗?
这是我的服务类
public class MyService extends JobService {
private List<String> resultList = new ArrayList<String>();
private int counter = 1;
JobScheduler jobScheduler;
@Override
public void onCreate() {
super.onCreate();
}
@Override
public boolean onStartJob(JobParameters jobParameters) {
addResultValues(jobParameters);
return true;
}
@Override
public boolean onStopJob(JobParameters jobParameters) {
jobScheduler = (JobScheduler)this.getSystemService(Context.JOB_SCHEDULER_SERVICE );
jobScheduler.cancelAll();
Toast.makeText(getApplicationContext(),"device stoppppppppppp..",Toast.LENGTH_SHORT).show();
return true;
}
private void addResultValues(JobParameters jobParameters ) {
Random random = new Random();
List<String> input = Arrays.asList("Linux", "Android","iPhone","Windows7" );
resultList.add(input.get(random.nextInt(3)) + " " + counter++);
if (counter == Integer.MAX_VALUE) {
counter = 0;
}
Log.e("service",resultList.size()+"::::::");
Toast.makeText(getApplicationContext(),"device charging..",Toast.LENGTH_SHORT).show();
jobFinished(jobParameters, false);
}
}
我的清单文件
<service android:name=".MyService"
android:label="app_name"
android:permission="android.permission.BIND_JOB_SERVICE"
>
</service>