/** * Create an intent that contains a task that should be scheduled * on a default queue. * @param context context instance * @param task task instance * @param <T> task type * @see #taskIntent(android.content.Context, String, java.util.concurrent.Callable) */ public static <T extends Callable<?> & Parcelable> Intent taskIntent(final Context context, final T task) { return taskIntent(context, Goro.DEFAULT_QUEUE, task); }
/** * Create an intent that contains a task that should be scheduled * on a default queue. * @param context context instance * @param task task instance * @param <T> task type * @see #taskIntent(android.content.Context, String, java.util.concurrent.Callable) */ public static <T extends Callable<?> & Parcelable> Intent taskIntent(final Context context, final T task) { return taskIntent(context, Goro.DEFAULT_QUEUE, task); }
@Override public void onReceive(final Context context, final Intent intent) { SampleApplication.graph(context).inject(this); NetworkInfo connection = connectivityManager.getActiveNetworkInfo(); if (connection != null && connection.isConnected()) { context.startService(GoroService.taskIntent(context, new TapeHandler())); } } }
@Test public void shouldScheduleTasksCreatedWithFactoryMethod() { service.onStartCommand(GoroService.taskIntent(application, task), 0, 1); queues.executeAll(); assertThat(executed).isTrue(); }
@Test public void canEatErrorsWhenAsked() throws Exception { Task task = mock(Task.class); Exception e = new Exception(); doThrow(e).when(task).call(); Intent intent = GoroService.taskIntent(application, task) .putExtra(GoroService.EXTRA_IGNORE_ERROR, true); service.onStartCommand(intent, 0, 1); queues.executeAll(); // and nothing happens }
@Test public void serviceContextShouldBeInjected() { Task task = mock(Task.class); service.onStartCommand(GoroService.taskIntent(application, task), 0, 1); verify(task).injectServiceContext(service); }
private void notificationSample() { Intent intent = GoroService.taskIntent(GoroActivity.this, QUEUE_REST, new PendingTask(counter++, QUEUE_REST)); PendingIntent pendingIntent = PendingIntent.getService(GoroActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).notify( 1, new Notification.Builder(GoroActivity.this) .setTicker("Click to post to REST") .setSmallIcon(R.drawable.ic_launcher) .setAutoCancel(true) .setContentTitle("Click to post to REST") .setContentText("Intent will be sent to the service") .setContentIntent(pendingIntent) .getNotification() ); }
@Test public void shouldNotEatErrors() throws Exception { Task task = mock(Task.class); Exception e = new Exception(); doThrow(e).when(task).call(); service.onStartCommand(GoroService.taskIntent(application, task), 0, 1); try { queues.executeAll(); fail("Error was not rethrown"); } catch (GoroException ge) { assertThat(ge.getCause()).isSameAs(e); } }