Android Services u0026 Background Tasks Fundamentals Quiz Quiz

Explore the essentials of Android services and background task execution with this quiz designed to assess your knowledge of foreground services, background processing strategies, service lifecycles, threading, and related best practices. Perfect for learners aiming to understand how Android handles tasks outside the main UI thread and how to implement efficient background operations.

  1. Basic Service Purpose

    What is the primary role of a Service in Android applications?

    1. To control the layout of user interface elements.
    2. To perform long-running operations in the background without a user interface.
    3. To display user interfaces and manage input events.
    4. To store large files and application data.

    Explanation: A Service is designed to run background operations that do not require a user interface, such as fetching network data or playing music. While Activities manage UI and user input, Services run without directly interacting with the UI. Storing files is not a service's job; storage APIs are used for that. Controlling layout is handled by UI components, not Services.

  2. Foreground Service Indicator

    Which requirement is necessary for a foreground service in Android to function properly?

    1. It should only be started from the main activity.
    2. It must display a persistent notification in the notification area.
    3. It must run only when the device screen is on.
    4. It requires access to external storage.

    Explanation: A foreground service must display an ongoing notification so that users are aware of background tasks affecting system resources. Running only with the screen on, starting only from the main activity, or requiring storage access are not requirements for foreground services. The persistent notification is crucial for transparency and compliance with background execution limits.

  3. Service Lifecycle Basics

    When using the startService() method, which method is called first in the service class?

    1. onBind()
    2. onDestroyView()
    3. onCreateView()
    4. onStartCommand()

    Explanation: onStartCommand() is called when a service is started with startService(), allowing it to handle incoming intents. onBind() is only for bound services, not started ones. onCreateView() and onDestroyView() are associated with fragments, not services, and thus are incorrect here.

  4. Bound Service Purpose

    What is the main advantage of using a bound service in an Android app?

    1. It replaces the need for content providers.
    2. It enables client components to interact directly with the service.
    3. It bypasses system resource limitations automatically.
    4. It ensures the service remains active indefinitely.

    Explanation: Bound services allow other components to bind and interact with them, enabling inter-process communication or in-app feature access. Such services stop when no clients are bound, so they don't run indefinitely. They do not bypass system limitations and are unrelated to content providers, which manage shared app data.

  5. Threading for Background Work

    Why should lengthy operations inside a Service usually be handled on a separate thread?

    1. To minimize the use of service lifecycle callbacks.
    2. To avoid storing data in shared preferences.
    3. To faster compile the application code.
    4. To prevent blocking the main thread and ensure the app remains responsive.

    Explanation: Running long tasks on the main thread can freeze the user interface and cause the system to flag the app as non-responsive. This has nothing to do with service callbacks, shared preferences storage, or code compilation speed. Proper threading ensures smooth app performance.

  6. IntentService Usage

    In Android, what is a primary benefit of using IntentService compared to a regular Service?

    1. It allows simultaneous binding and starting by default.
    2. It has a higher execution priority than normal services.
    3. It updates the user interface directly from its background thread.
    4. It automatically handles each intent on a worker thread and stops itself when done.

    Explanation: IntentService manages a worker thread, so tasks don't block the main thread, and it stops itself after all work finishes. It does not update the UI directly, since background threads cannot interact with UI elements. IntentService isn't intended for binding, and it doesn't have a higher execution priority than regular services.

  7. Choosing Background Solutions

    Which built-in component is best suited for scheduling deferrable, background tasks that need guaranteed execution, even after device restarts?

    1. ViewPager
    2. WorkManager
    3. AsyncTask
    4. RecyclerView

    Explanation: WorkManager is ideal for deferrable background work that must run reliably, handling tasks across app restarts and device reboots. AsyncTask is for short-lived tasks tied to a component's lifecycle, not persistent jobs. RecyclerView and ViewPager are UI components, not background task schedulers.

  8. Service Persistence

    What happens if the system kills a started service due to low memory and the service has a START_STICKY return value?

    1. The original intent is always re-delivered after restart.
    2. The service's task is permanently stopped.
    3. The service is not restarted by the system.
    4. The system recreates the service as soon as possible, but the last intent is not re-delivered.

    Explanation: With START_STICKY, the system attempts to recreate the service, but without redelivering the original start intent. The service is not left unrestarted nor is its job considered permanently stopped. Only START_REDELIVER_INTENT ensures the intent is resent on restart.

  9. Background Execution Limitation

    On newer Android versions, what can happen if a non-foreground service tries to run in the background for an extended period?

    1. The notification bar disappears.
    2. It bypasses power-saving features by default.
    3. The system may stop the service to save resources.
    4. The service automatically switches to foreground mode.

    Explanation: To improve battery life and system resources, Android limits how long non-foreground services run in the background, often stopping them if they persist too long. Services do not automatically become foreground services; explicit code is needed. Background services can't make the notification bar disappear or bypass power-saving features.

  10. Sticky vs. Non-Sticky Services

    Which statement accurately describes a service started with START_NOT_STICKY?

    1. It is guaranteed to restart immediately after being killed.
    2. It will resume with the same state automatically.
    3. It cannot be stopped manually.
    4. If killed, the service will only be recreated if there are pending start requests.

    Explanation: START_NOT_STICKY means the system won't restart the service unless new explicit start requests come in. There's no guarantee of immediate restart, and the service can still be stopped manually. The service does not automatically resume its previous state without developer intervention.