Search in sources :

Example 1 with JobScheduler

use of android.app.job.JobScheduler in project cw-omnibus by commonsguy.

the class EventDemoActivity method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getFragmentManager().findFragmentById(android.R.id.content) == null) {
        getFragmentManager().beginTransaction().add(android.R.id.content, new EventLogFragment()).commit();
        JobScheduler jobs = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
        ComponentName cn = new ComponentName(this, ScheduledService.class);
        JobInfo.Builder b = new JobInfo.Builder(JOB_ID, cn).setRequiredNetworkType(JobInfo.NETWORK_TYPE_NONE).setPeriodic(60000).setPersisted(false).setRequiresCharging(false).setRequiresDeviceIdle(false);
        jobs.schedule(b.build());
    }
}
Also used : JobScheduler(android.app.job.JobScheduler) JobInfo(android.app.job.JobInfo) ComponentName(android.content.ComponentName)

Example 2 with JobScheduler

use of android.app.job.JobScheduler in project materialistic by hidroh.

the class SyncDelegate method scheduleSync.

@UiThread
public static void scheduleSync(Context context, Job job) {
    if (!Preferences.Offline.isEnabled(context)) {
        return;
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && !TextUtils.isEmpty(job.id)) {
        JobInfo.Builder builder = new JobInfo.Builder(Long.valueOf(job.id).intValue(), new ComponentName(context.getPackageName(), ItemSyncJobService.class.getName())).setRequiredNetworkType(Preferences.Offline.isWifiOnly(context) ? JobInfo.NETWORK_TYPE_UNMETERED : JobInfo.NETWORK_TYPE_ANY).setExtras(job.toPersistableBundle());
        if (Preferences.Offline.currentConnectionEnabled(context)) {
            builder.setOverrideDeadline(0);
        }
        ((JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE)).schedule(builder.build());
    } else {
        Bundle extras = new Bundle(job.toBundle());
        extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
        Account syncAccount;
        AccountManager accountManager = AccountManager.get(context);
        Account[] accounts = accountManager.getAccountsByType(BuildConfig.APPLICATION_ID);
        if (accounts.length == 0) {
            syncAccount = new Account(SYNC_ACCOUNT_NAME, BuildConfig.APPLICATION_ID);
            accountManager.addAccountExplicitly(syncAccount, null, null);
        } else {
            syncAccount = accounts[0];
        }
        ContentResolver.requestSync(syncAccount, MaterialisticProvider.PROVIDER_AUTHORITY, extras);
    }
}
Also used : JobScheduler(android.app.job.JobScheduler) Account(android.accounts.Account) JobInfo(android.app.job.JobInfo) Bundle(android.os.Bundle) PersistableBundle(android.os.PersistableBundle) ComponentName(android.content.ComponentName) AccountManager(android.accounts.AccountManager) UiThread(android.support.annotation.UiThread)

Example 3 with JobScheduler

use of android.app.job.JobScheduler in project materialistic by hidroh.

the class ItemSyncJobServiceTest method testScheduledJob.

@Test
public void testScheduledJob() {
    PreferenceManager.getDefaultSharedPreferences(RuntimeEnvironment.application).edit().putBoolean(RuntimeEnvironment.application.getString(R.string.pref_saved_item_sync), true).apply();
    shadowOf((ConnectivityManager) RuntimeEnvironment.application.getSystemService(Context.CONNECTIVITY_SERVICE)).setActiveNetworkInfo(ShadowNetworkInfo.newInstance(null, ConnectivityManager.TYPE_WIFI, 0, true, true));
    SyncDelegate.scheduleSync(RuntimeEnvironment.application, new SyncDelegate.JobBuilder(RuntimeEnvironment.application, "1").build());
    List<JobInfo> pendingJobs = shadowOf((JobScheduler) RuntimeEnvironment.application.getSystemService(Context.JOB_SCHEDULER_SERVICE)).getAllPendingJobs();
    assertThat(pendingJobs).isNotEmpty();
    JobInfo actual = pendingJobs.get(0);
    assertThat(actual.getService().getClassName()).isEqualTo(ItemSyncJobService.class.getName());
}
Also used : JobScheduler(android.app.job.JobScheduler) JobInfo(android.app.job.JobInfo) ConnectivityManager(android.net.ConnectivityManager) Test(org.junit.Test)

Example 4 with JobScheduler

use of android.app.job.JobScheduler in project muzei by romannurik.

the class ArtworkComplicationJobService method scheduleComplicationUpdateJob.

static void scheduleComplicationUpdateJob(Context context) {
    JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
    ComponentName componentName = new ComponentName(context, ArtworkComplicationJobService.class);
    int result = jobScheduler.schedule(new JobInfo.Builder(ARTWORK_COMPLICATION_JOB_ID, componentName).addTriggerContentUri(new JobInfo.TriggerContentUri(MuzeiContract.Artwork.CONTENT_URI, JobInfo.TriggerContentUri.FLAG_NOTIFY_FOR_DESCENDANTS)).build());
    if (BuildConfig.DEBUG) {
        Log.d(TAG, "Job scheduled with " + (result == JobScheduler.RESULT_SUCCESS ? "success" : "failure"));
    }
    // Enable the BOOT_COMPLETED receiver to reschedule the job on reboot
    ComponentName bootReceivedComponentName = new ComponentName(context, ArtworkComplicationBootReceiver.class);
    context.getPackageManager().setComponentEnabledSetting(bootReceivedComponentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
}
Also used : JobScheduler(android.app.job.JobScheduler) JobInfo(android.app.job.JobInfo) ComponentName(android.content.ComponentName)

Example 5 with JobScheduler

use of android.app.job.JobScheduler in project muzei by romannurik.

the class ArtworkComplicationJobService method cancelComplicationUpdateJob.

static void cancelComplicationUpdateJob(Context context) {
    JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
    jobScheduler.cancel(ARTWORK_COMPLICATION_JOB_ID);
    if (BuildConfig.DEBUG) {
        Log.d(TAG, "Job cancelled");
    }
    // Disable the BOOT_COMPLETED receiver to reduce memory pressure on boot
    ComponentName bootReceivedComponentName = new ComponentName(context, ArtworkComplicationBootReceiver.class);
    context.getPackageManager().setComponentEnabledSetting(bootReceivedComponentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
}
Also used : JobScheduler(android.app.job.JobScheduler) ComponentName(android.content.ComponentName)

Aggregations

JobScheduler (android.app.job.JobScheduler)63 JobInfo (android.app.job.JobInfo)42 ComponentName (android.content.ComponentName)13 NotificationManager (android.app.NotificationManager)5 Calendar (java.util.Calendar)5 Random (java.util.Random)5 AlarmManager (android.app.AlarmManager)2 SharedPreferences (android.content.SharedPreferences)2 Account (android.accounts.Account)1 AccountManager (android.accounts.AccountManager)1 SuppressLint (android.annotation.SuppressLint)1 ConnectivityManager (android.net.ConnectivityManager)1 Bundle (android.os.Bundle)1 PersistableBundle (android.os.PersistableBundle)1 UiThread (android.support.annotation.UiThread)1 Test (org.junit.Test)1