0

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?

Wrichik Basu
  • 1,005
  • 17
  • 28

1 Answers1

0

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • In which API is `IntentService` deprecated? Also, if I create another thread from `onHandleIntent()`, will that thread stay alive with the foreground service? – Wrichik Basu Mar 28 '20 at 13:37
  • @WrichikBasu: "In which API is IntentService deprecated?" -- it is being deprecated as part of Android R (see [the JavaDocs](https://developer.android.com/reference/android/app/IntentService)). "Also, if I create another thread from onHandleIntent(), will that thread stay alive with the foreground service?" -- for as long as your process does, yes. IMHO, though, this is starting to really sound like you should not be using `IntentService`. If you want to fork your own threads, use a regular `Service`. – CommonsWare Mar 28 '20 at 13:47