androidandroid-intentserviceforeground-service

Does Android foreground IntentService run in the UI thread or a different thread?


I am making an IntentService. The code is something like this:

protected void onHandleIntent(@Nullable Intent intent) {
    startForeground(NOTIFICATION_ID, buildForegroundNotification());
}


private Notification buildForegroundNotification() {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), Integer.toString(NOTIFICATION_ID))
            .setContentTitle("App is running.")
            .setContentText("")
            .setPriority(NotificationCompat.PRIORITY_HIGH);

    return (builder.build());
}

Normally IntentService creates a separate worker thread for its service. But here, I am calling this service as a foreground one. Will this service work on the main UI thread, or create a separate thread?


Solution

  • I am making an IntentService

    Note that IntentService is deprecated.

    Will this service work on the main UI thread, or create a separate thread?

    onHandleIntent() will be called on a background thread, regardless of whether or not you make it a foreground service.