TrackingService extends Service { public static void startAndBindService(Context context, Class<?> cls, ServiceConnection connection, TrackingServiceOptions options) { Intent mIntent = TrackingService.getIntent(context, cls, options); TrackingService.bindService(context, mIntent, connection); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { context.startForegroundService(mIntent); } else { context.startService(mIntent); } }  @Override void onCreate(); @Override int onStartCommand(Intent intent, int flags, int startId); @Override void onDestroy(); @Override IBinder onBind(Intent intent); void registerTrackingServiceListener(@NonNull TrackingServiceListener listener); void unregisterTrackingServiceListener(); List<KujakuLocation> getRecordedKujakuLocations(); void setTag(long tag); void takeLocation(long tag); static void startAndBindService(Context context, Class<?> cls, ServiceConnection connection, TrackingServiceOptions options); static boolean bindService(Context context, Intent intent, ServiceConnection connection); static Intent getIntent(Context context, Class<?> cls, TrackingServiceOptions options); static void stopAndUnbindService(Context context, ServiceConnection connection); static void unBindService(Context context, ServiceConnection connection); static boolean isRunning(); static int getTrackingServiceStatus(); static List<KujakuLocation> getCurrentRecordedKujakuLocations(); static List<KujakuLocation> getPreviousRecordedKujakuLocations(); static int NO_FORCED_TAG; }
@Test public void testStartAndBindService() throws InterruptedException { CountDownLatch latch1 = new CountDownLatch(1); CountDownLatch latch2 = new CountDownLatch(1); ServiceConnection connection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { connectionStatus = "connected"; latch1.countDown(); } @Override public void onServiceDisconnected(ComponentName name) { connectionStatus = "disconnected"; latch2.countDown(); } }; assertEquals(connectionStatus, ""); TrackingService.startAndBindService(context, MapActivity.class, connection, new TrackingServiceHighAccuracyOptions()); latch1.await(); assertEquals(connectionStatus, "connected"); TrackingService.stopAndUnbindService(context, connection); latch2.await(); assertEquals(connectionStatus, "disconnected"); }