將Android Lollipop的JobScheduler向后移植至API 10+
Android棒棒糖的的jobscheduler反向移植到API 10+。所有的jobscheduler功能已實現。然而,這個庫一直沒有經過充分測試的,所以我會建議不要在此時在生產中使用。我們不能保證這不會耗盡你的電池或導致設備發生爆炸。
用法
The api is identical to the officialJobScheduler, the only differences will be what you import.
First create a service to run your job.
import me.tatarka.support.job.JobParameters; import me.tatarka.support.job.JobService; /** * Service to handle callbacks from the JobScheduler. Requests scheduled with the JobScheduler * ultimately land on this service's "onStartJob" method. */ public class TestJobService extends JobService { @Override public boolean onStartJob(JobParameters params) { // Start your job in a seperate thread, calling jobFinished(params, needsRescheudle) when you are done. // See the javadoc for more detail. return true; } @Override public boolean onStopJob(JobParameters params) { // Stop the running job, returing true if it needs to be recheduled. // See the javadoc for more detail. return true; }
Then use the JobScheduler
to schedule the job.
import me.tatarka.support.job.JobInfo; import me.tatarka.support.job.JobScheduler; import me.tatarka.support.os.PersistableBundle; // Get an instance of the JobScheduler, this will delegate to the system JobScheduler on api 21+ // and to a custom implementataion on older api levels. JobScheduler jobScheduler = JobScheduler.getInstance(context); // Extras for your job. PersitableBundle extras = new PersitableBundle(); extras.putString("key", "value"); // Construct a new job with your service and some constraints. // See the javadoc for more detail. JobInfo job = new JobInfo.Builder(0 /*jobid*/, new ComponentName(context, TestJobService.class)) .setMinimumLatency(1000) .setOverrideDeadline(2000) .setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED) .setRequiresCharging(true) .setExtras(extras) .build(); jobScheudler.scheduleJob(job);
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!