本文整理汇总了Java中android.app.job.JobScheduler类的典型用法代码示例。如果您正苦于以下问题:Java JobScheduler类的具体用法?Java JobScheduler怎么用?Java JobScheduler使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
JobScheduler类属于android.app.job包,在下文中一共展示了JobScheduler类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: onSharedPreferenceChanged
import android.app.job.JobScheduler; //导入依赖的package包/类
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
final String remindersKey = getString(R.string.pref_key_reminders);
if (key.equals(remindersKey)) {
boolean enabled = sharedPreferences.getBoolean(remindersKey, false);
JobScheduler jobScheduler =
(JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
if (!enabled) {
jobScheduler.cancel(JOB_ID);
Log.d(TAG, "cancelling scheduled job");
} else {
long interval = AlarmManager.INTERVAL_HOUR;
JobInfo job = new JobInfo.Builder(JOB_ID,
new ComponentName(getPackageName(),
ScheduledJobService.class.getName()))
.setPersisted(true)
.setPeriodic(interval)
.build();
jobScheduler.schedule(job);
Log.d(TAG, "setting scheduled job for: " + interval);
}
}
}
示例2: onReceive
import android.app.job.JobScheduler; //导入依赖的package包/类
@Override
public void onReceive(Context context, Intent intent) {
JobScheduler jobScheduler =
(JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
// If there are not pending jobs. Create a sync job and schedule it.
List<JobInfo> pendingJobs = jobScheduler.getAllPendingJobs();
if (pendingJobs.isEmpty()) {
String inputId = context.getSharedPreferences(SyncJobService.PREFERENCE_EPG_SYNC,
Context.MODE_PRIVATE).getString(SyncJobService.BUNDLE_KEY_INPUT_ID, null);
if (inputId != null) {
// Set up periodic sync only when input has set up.
SyncUtils.setUpPeriodicSync(context, inputId);
}
return;
}
// On L/L-MR1, reschedule the pending jobs.
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1) {
for (JobInfo job : pendingJobs) {
if (job.isPersisted()) {
jobScheduler.schedule(job);
}
}
}
}
示例3: scheduleAddWatchNextRequest
import android.app.job.JobScheduler; //导入依赖的package包/类
public static void scheduleAddWatchNextRequest(Context context, ClipData clipData) {
JobScheduler scheduler = (JobScheduler) context.getSystemService(JOB_SCHEDULER_SERVICE);
PersistableBundle bundle = new PersistableBundle();
bundle.putString(ID_KEY, clipData.getClipId());
bundle.putString(CONTENT_ID_KEY, clipData.getContentId());
bundle.putLong(DURATION_KEY, clipData.getDuration());
bundle.putLong(PROGRESS_KEY, clipData.getProgress());
bundle.putString(TITLE_KEY, clipData.getTitle());
bundle.putString(DESCRIPTION_KEY, clipData.getDescription());
bundle.putString(CARD_IMAGE_URL_KEY, clipData.getCardImageUrl());
scheduler.schedule(new JobInfo.Builder(1,
new ComponentName(context, AddWatchNextService.class))
.setMinimumLatency(0)
.setExtras(bundle)
.build());
}
示例4: schedule
import android.app.job.JobScheduler; //导入依赖的package包/类
@SuppressWarnings("ConstantConditions")
public static void schedule(final Context context) {
final JobScheduler scheduler = context.getSystemService(JobScheduler.class);
for (final JobInfo job : scheduler.getAllPendingJobs()) {
if (job.getId() == JOB_ID_PERIODIC) {
return;
}
}
final long interval = MINUTE *
Integer.valueOf(DefaultSharedPrefUtils.getBackgroundServiceInterval(context));
final ComponentName name = new ComponentName(context, PeriodicJob.class);
final int result = scheduler.schedule(new JobInfo.Builder(JOB_ID_PERIODIC, name)
.setPeriodic(interval)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
.build());
if (result != JobScheduler.RESULT_SUCCESS) {
Log.e(TAG, "Failed to schedule periodic job");
}
}
示例5: onCreate
import android.app.job.JobScheduler; //导入依赖的package包/类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Create JobScheduler
JobScheduler jobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
//Create a component passing the JobService that we want to use
ComponentName jobService = new ComponentName(getPackageName(), MyJobService.class.getName());
//Create a JobInfo passing a unique JOB_ID and the jobService
//also set the periodic time to repeat this job
JobInfo jobInfo = new JobInfo.Builder(JOB_ID, jobService)
.setPeriodic(REFRESH_INTERVAL)
.build();
jobScheduler.schedule(jobInfo);
}
示例6: setServiceEnable
import android.app.job.JobScheduler; //导入依赖的package包/类
/**
* Set XMPush sdk enable
* @param enable enable
* @param context context param
*/
public static void setServiceEnable (boolean enable, Context context) {
if (enable && isAppMainProc(context)) {
MiPushClient.registerPush(wrapContext(context), APP_ID, APP_KEY);
} else {
MiPushClient.unregisterPush(wrapContext(context));
// Force stop and disable services.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
JobScheduler scheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
scheduler.cancelAll();
}
context.stopService(new Intent(context, XMPushService.class));
}
}
示例7: testProcessOnePacket
import android.app.job.JobScheduler; //导入依赖的package包/类
@Test(timeout = 5000)
public void testProcessOnePacket() throws Exception {
DataPacket dataPacket = new ByteArrayDataPacket(Collections.singletonMap("id", "testId"), "testPayload".getBytes(Charsets.UTF_8));
queuedSiteToSiteClientConfig.createQueuedClient(context).enqueue(dataPacket);
mockNiFiS2SServer.enqueueSiteToSitePeers(Collections.singletonList(peer));
String transactionPath = mockNiFiS2SServer.enqueuCreateTransaction(portIdentifier, transactionIdentifier, 30);
mockNiFiS2SServer.enqueuDataPackets(transactionPath, Collections.singletonList(dataPacket), queuedSiteToSiteClientConfig);
mockNiFiS2SServer.enqueueTransactionComplete(transactionPath, 2, ResponseCode.CONFIRM_TRANSACTION, ResponseCode.CONFIRM_TRANSACTION);
JobInfo.Builder processJobInfoBuilder = SiteToSiteJobService.createProcessJobInfoBuilder(context, 1, queuedSiteToSiteClientConfig, parcelableQueuedOperationResultCallback);
processJobInfoBuilder.setOverrideDeadline(0);
JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
assertEquals(JobScheduler.RESULT_SUCCESS, jobScheduler.schedule(processJobInfoBuilder.build()));
assertEquals(1, parcelableQueuedOperationResultCallback.getInvocations().size());
SiteToSiteDBTestUtil.assertNoQueuedPackets(siteToSiteDB);
mockNiFiS2SServer.verifyAssertions();
}
示例8: testStart
import android.app.job.JobScheduler; //导入依赖的package包/类
@Test
public void testStart() {
Context context = InstrumentationRegistry.getTargetContext();
QuickPeriodicJobScheduler qpjs = new QuickPeriodicJobScheduler(context);
qpjs.start(2, 30000l);
SystemClock.sleep(1000);
JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
List<JobInfo> jobInfoList = jobScheduler.getAllPendingJobs();
JobInfo jobInfo = null;
for(JobInfo job : jobInfoList) {
if(job.getId() == 2) {
jobInfo = job;
}
}
Assert.assertEquals(jobInfo.getMaxExecutionDelayMillis(), 30000l);
Assert.assertEquals(jobInfo.getMinLatencyMillis(), 30000l);
Assert.assertEquals(jobInfo.getId(), 2);
Assert.assertEquals(jobInfo.getExtras().getLong("interval"), 30000l);
Assert.assertNotNull(jobInfo);
}
示例9: schedule
import android.app.job.JobScheduler; //导入依赖的package包/类
public static void schedule(Context context) {
SharedPreferences settings = AppSettings.getSharedPreferences(context);
int notificationsFrequency = AppSettings.Notifications.getNotificationsFrequency(settings);
ComponentName component = new ComponentName(context, NotificationsJobService.class);
JobInfo.Builder builder = new JobInfo.Builder(JOB_ID, component)
.setPeriodic(60000 * notificationsFrequency);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_NOT_ROAMING);
else
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
jobScheduler.schedule(builder.build());
}
示例10: startPolling
import android.app.job.JobScheduler; //导入依赖的package包/类
@TargetApi(21)
public static void startPolling(Context context) {
JobScheduler scheduler = (JobScheduler)
context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
final int JOB_ID = 1;
if (isBeenScheduled(JOB_ID, context)){
Log.i(TAG, "scheduler.cancel(JOB_ID)");
scheduler.cancel(JOB_ID);
} else{
Log.i(TAG, "scheduler.schedule(jobInfo)");
int pollInterval = QueryPreferences.getPollInterval(context);
Log.i(TAG, "the poll interval is: " + pollInterval + " ms");
JobInfo jobInfo = new JobInfo.Builder(
JOB_ID, new ComponentName(context, PollJobService.class))
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
.setPeriodic(pollInterval)
.setPersisted(true)
.build();
scheduler.schedule(jobInfo);
}
}
示例11: onCreate
import android.app.job.JobScheduler; //导入依赖的package包/类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.job_list_activity);
RecyclerView rv = (RecyclerView)findViewById(R.id.jobList);
// Set the recycler view layout
LinearLayoutManager llm = new LinearLayoutManager(this);
rv.setLayoutManager(llm);
initList();
Button cancelAllBut = (Button)findViewById(R.id.cancelAllBut);
cancelAllBut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
JobScheduler jobScheduler = (JobScheduler)getSystemService(JOB_SCHEDULER_SERVICE);
jobScheduler.cancelAll();
initList();
Toast.makeText(JobListActivity.this,
"Cancelling all the pending jobs",
Toast.LENGTH_LONG).show();
}
});
}
示例12: onBindViewHolder
import android.app.job.JobScheduler; //导入依赖的package包/类
@Override
public void onBindViewHolder(JobListRecyclerAdapter.JobViewHolder holder, int position) {
final JobInfo ji= mJobList.get(position);
holder.jobId.setText(Integer.toString(ji.getId()));
holder.serviceName.setText(ji.getService().getClassName());
holder.stopBut.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
JobScheduler jobScheduler = (JobScheduler)mContext.getSystemService(mContext.JOB_SCHEDULER_SERVICE);
jobScheduler.cancel(ji.getId());
Log.i("JobList", "Stopping the job "+ji.getId());
Toast.makeText(mContext,
"Canceling the job "+ji.getId(),
Toast.LENGTH_LONG).show();
mContext.initList();
}
});
}
开发者ID:PacktPublishing,项目名称:Asynchronous-Android-Programming,代码行数:20,代码来源:JobListRecyclerAdapter.java
示例13: schedulePeriodic
import android.app.job.JobScheduler; //导入依赖的package包/类
private static void schedulePeriodic(Context context) {
Timber.d("Scheduling a periodic task");
JobInfo.Builder builder = new JobInfo.Builder(
PERIODIC_ID, new ComponentName(context, QuoteJobService.class));
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
.setPeriodic(PERIOD)
.setBackoffCriteria(INITIAL_BACKOFF, JobInfo.BACKOFF_POLICY_EXPONENTIAL);
JobScheduler scheduler = (JobScheduler) context.getSystemService(
Context.JOB_SCHEDULER_SERVICE);
int result = scheduler.schedule(builder.build());
if (result == JobScheduler.RESULT_SUCCESS) {
Timber.i("Job scheduled successfully!");
} else {
Timber.e("Job did not scheduled!");
}
}
示例14: syncJob
import android.app.job.JobScheduler; //导入依赖的package包/类
private void syncJob() {
QiscusAccount qiscusAccount = Qiscus.getQiscusAccount();
Random rand = new Random();
int randomValue = rand.nextInt(50);
JobInfo jobInfo = new JobInfo.Builder(qiscusAccount.getId() + randomValue, componentName)
.setPeriodic(TimeUnit.MINUTES.toMillis(15))
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
.build();
JobScheduler jobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
if (jobScheduler != null) {
jobScheduler.schedule(jobInfo);
}
}
示例15: registerForNetworkAvailability
import android.app.job.JobScheduler; //导入依赖的package包/类
private static void registerForNetworkAvailability(Context context) {
JobScheduler jobScheduler = (JobScheduler) context.getSystemService(JOB_SCHEDULER_SERVICE);
if (jobScheduler == null) {
return;
}
int scheduleId = getScheduleId(context, ON_NETWORK_AVAILABLE_JOB_ID);
jobScheduler.cancel(scheduleId);
int r = jobScheduler.schedule(new JobInfo.Builder(scheduleId, new ComponentName(context, MobileMessagingJobService.class))
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
.build());
if (r == JobScheduler.RESULT_SUCCESS) {
MobileMessagingLogger.d(TAG, "Registered job for connectivity updates");
} else {
MobileMessagingLogger.e(TAG, "Failed to register job for connectivity updates");
}
}