作业调度程序停止工作,如果应用程序死亡android
问题描述:
您在我的应用程序中做了JobScheduler
的示例。 这是我如何启动它作业调度程序停止工作,如果应用程序死亡android
jobScheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
ComponentName jobService = new ComponentName(getPackageName(),
MyJobService.class.getName());
JobInfo jobInfo = new JobInfo.Builder(MYJOBID, jobService)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
.setExtras(bundle).build();
jobScheduler.schedule(jobInfo);
而且我发现在JobService
举杯:
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class MyJobService extends JobService {
public MyJobService() {
}
@Override
public boolean onStartJob(JobParameters params) {
// UtilityMethods.showToast(this,params.getExtras().getString("json"));
Toast.makeText(this,"test",Toast.LENGTH_SHORT).show();
return false;
}
@Override
public boolean onStopJob(JobParameters params) {
UtilityMethods.showToast(this,"onStop()");
return false;
}
}
,这是工作完全正常的,甚至我试图关闭网络和后台查杀应用程序。 然后我尝试在我的一个库中创建类似的东西。我在库中编写了相同的代码,我从我的应用程序MainActivity
中调用它。但这一次,当我从后台杀死应用程序,它停止工作。谁能告诉我为什么?
我MainActivity
,我将其初始化
JobScheduler jobScheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
ComponentName jobService = new ComponentName(getPackageName(),
MyJobService.class.getName());
JobInfo jobInfo = new JobInfo.Builder(MYJOBID, jobService)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY).build();
jobScheduler.schedule(jobInfo);
它工作时,我从开始的onCreate它,而不是工作,如果我从一个回调funtion启动它()。
任何帮助真的很感激。
答
制作这回真
@Override
public boolean onStartJob(JobParameters params) {
// UtilityMethods.showToast(this,params.getExtras().getString("json"));
Toast.makeText(this,"test",Toast.LENGTH_SHORT).show();
return true;
}
,并从这里开始一个新的线程(因为这是在mainthread只执行)。
onStartJob
added in API level 21
boolean onStartJob (JobParameters params)
Override this method with the callback logic for your job. Any such logic needs to be performed on a separate thread, as this function is executed on your application's main thread.
Parameters
params JobParameters: Parameters specifying info about this job, including the extras bundle you optionally provided at job-creation time.
Returns
boolean True if your service needs to process the work (on a separate thread). False if there's no more work to be done for this job.
我不明白..怎么可能呢?你能解释一下吗? –
我已经添加谷歌页面的解释,请让我知道,如果它不清楚给你。 –