백그라운드 서비스 만들기
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
IntentService
클래스는 실행을 위한 간단한 구조를 제공합니다.
단일 백그라운드 스레드에서 수행됩니다. 따라서 장기 실행 작업을 처리할 수 있습니다.
새 버전을 출시할 수 있습니다 또한
IntentService
는 대부분의 사용자 인터페이스 수명 주기 이벤트의 영향을 받지 않으므로
AsyncTask
를 종료하는 상황에서도 계속 실행됩니다.
IntentService
에는 다음과 같은 몇 가지 제한사항이 있습니다.
-
사용자 인터페이스와 직접 상호작용할 수 없습니다. UI에 결과를 넣으려면
Activity
로 전송해야 합니다.
-
작업 요청이 순차적으로 실행됩니다. 클러스터에서 작업이 실행 중인 경우
IntentService
를 호출하고 다른 요청을 보내면 이 요청은 다음 시간까지 대기합니다.
첫 번째 작업이 완료됩니다
-
IntentService
에서 실행되는 작업은 중단할 수 없습니다.
그러나 대부분의 경우 IntentService
를 사용하는 것이 선호됩니다.
간단한 백그라운드 작업을
수행할 수 있습니다
이 가이드에서는 다음 작업을 실행하는 방법을 보여줍니다.
수신 인텐트 처리
앱의 IntentService
구성요소를 만들려면 다음과 같은 클래스를 정의합니다.
IntentService
를 확장하고 그 안에서
onHandleIntent()
를 재정의합니다. 예를 들면 다음과 같습니다.
Kotlin
class RSSPullService : IntentService(RSSPullService::class.simpleName)
override fun onHandleIntent(workIntent: Intent) {
// Gets data from the incoming Intent
val dataString = workIntent.dataString
...
// Do work here, based on the contents of dataString
...
}
}
자바
public class RSSPullService extends IntentService {
@Override
protected void onHandleIntent(Intent workIntent) {
// Gets data from the incoming Intent
String dataString = workIntent.getDataString();
...
// Do work here, based on the contents of dataString
...
}
}
다음과 같은 일반 Service
구성요소의 다른 콜백은 다음과 같습니다.
onStartCommand()
는
IntentService
입니다. IntentService
에서는 다음을 피해야 합니다.
이러한 콜백을 재정의합니다.
IntentService
생성에 관한 자세한 내용은
IntentService 클래스를 통해 할당됩니다.
매니페스트에서 인텐트 서비스 정의
IntentService
는 애플리케이션 매니페스트에도 항목이 필요합니다.
이 항목을
<service>
다음 요소의 하위 요소인
<application>
요소:
<application
android:icon="@drawable/icon"
android:label="@string/app_name">
...
<!--
Because android:exported is set to "false",
the service is only available to this app.
-->
<service
android:name=".RSSPullService"
android:exported="false"/>
...
</application>
android:name
속성은
IntentService
입니다.
참고로
<service>
요소가 포함되지 않은 요소
인텐트 필터. 이
서비스에 작업 요청을 보내는 Activity
는 다음을 사용합니다.
명시적 Intent
이므로 필터가 필요하지 않습니다. 또한
같은 앱 또는
서비스에 액세스할 수 있습니다
이제 기본 IntentService
클래스가 있으므로 작업 요청을 보낼 수 있습니다.
Intent
객체로 변환합니다. 이러한 객체를 구성하는 절차
IntentService
로 전송하는 방법은 다음 페이지를 참고하세요.
백그라운드 서비스에 작업 요청을 보냅니다.
이 페이지에 나와 있는 콘텐츠와 코드 샘플에는 콘텐츠 라이선스에서 설명하는 라이선스가 적용됩니다. 자바 및 OpenJDK는 Oracle 및 Oracle 계열사의 상표 또는 등록 상표입니다.
최종 업데이트: 2025-07-27(UTC)
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["필요한 정보가 없음","missingTheInformationINeed","thumb-down"],["너무 복잡함/단계 수가 너무 많음","tooComplicatedTooManySteps","thumb-down"],["오래됨","outOfDate","thumb-down"],["번역 문제","translationIssue","thumb-down"],["샘플/코드 문제","samplesCodeIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-07-27(UTC)"],[],[],null,["# Create a background service\n\n| **Note:** `IntentService` will not work correctly when the application is in the background on the latest versions of Android. This page is left here as reference for legacy apps only. See the [guide to background processing\n| on Android](/guide/background) for recommended solutions.\n\n\nThe [IntentService](/reference/android/app/IntentService) class provides a straightforward structure for running\nan operation on a single background thread. This allows it to handle long-running operations\nwithout affecting your user interface's responsiveness. Also, an\n[IntentService](/reference/android/app/IntentService) isn't affected by most user interface lifecycle events, so it\ncontinues to run in circumstances that would shut down an [AsyncTask](/reference/android/os/AsyncTask)\n\n\nAn [IntentService](/reference/android/app/IntentService) has a few limitations:\n\n- It can't interact directly with your user interface. To put its results in the UI, you have to send them to an [Activity](/reference/android/app/Activity).\n- Work requests run sequentially. If an operation is running in an [IntentService](/reference/android/app/IntentService), and you send it another request, the request waits until the first operation is finished.\n- An operation running on an [IntentService](/reference/android/app/IntentService) can't be interrupted.\n\n\nHowever, in most cases an [IntentService](/reference/android/app/IntentService) is the preferred way to perform\nsimple background operations.\n\n\nThis guide shows you how to do the following things:\n\n- Create your own subclass of [IntentService](/reference/android/app/IntentService).\n- Create the required callback method [onHandleIntent()](/reference/android/app/IntentService#onHandleIntent(android.content.Intent)).\n- Define the [IntentService](/reference/android/app/IntentService) in your manifest file.\n\nHandle incoming intents\n-----------------------\n\n\nTo create an [IntentService](/reference/android/app/IntentService) component for your app, define a class that\nextends [IntentService](/reference/android/app/IntentService), and within it, define a method that\noverrides [onHandleIntent()](/reference/android/app/IntentService#onHandleIntent(android.content.Intent)). For example: \n\n### Kotlin\n\n```kotlin\nclass RSSPullService : IntentService(RSSPullService::class.simpleName)\n\n override fun onHandleIntent(workIntent: Intent) {\n // Gets data from the incoming Intent\n val dataString = workIntent.dataString\n ...\n // Do work here, based on the contents of dataString\n ...\n }\n}\n```\n\n### Java\n\n```java\npublic class RSSPullService extends IntentService {\n @Override\n protected void onHandleIntent(Intent workIntent) {\n // Gets data from the incoming Intent\n String dataString = workIntent.getDataString();\n ...\n // Do work here, based on the contents of dataString\n ...\n }\n}\n```\n\n\nNotice that the other callbacks of a regular [Service](/reference/android/app/Service) component, such as\n[onStartCommand()](/reference/android/app/Service#onStartCommand(android.content.Intent, int, int)) are automatically invoked by\n[IntentService](/reference/android/app/IntentService). In an [IntentService](/reference/android/app/IntentService), you should avoid\noverriding these callbacks.\n\nTo learn more about creating an `IntentService`, see [Extending the\nIntentService class](/guide/components/services#ExtendingIntentService).\n\nDefine the intent service in the manifest\n-----------------------------------------\n\n\nAn [IntentService](/reference/android/app/IntentService) also needs an entry in your application manifest.\nProvide this entry as a\n[\u003cservice\u003e](/guide/topics/manifest/service-element)\nelement that's a child of the\n[\u003capplication\u003e](/guide/topics/manifest/application-element) element: \n\n```xml\n \u003capplication\n android:icon=\"@drawable/icon\"\n android:label=\"@string/app_name\"\u003e\n ...\n \u003c!--\n Because android:exported is set to \"false\",\n the service is only available to this app.\n --\u003e\n \u003cservice\n android:name=\".RSSPullService\"\n android:exported=\"false\"/\u003e\n ...\n \u003c/application\u003e\n```\n\n\nThe attribute `android:name` specifies the class name of the\n[IntentService](/reference/android/app/IntentService).\n\n\nNotice that the\n[\u003cservice\u003e](/guide/topics/manifest/service-element)\nelement doesn't contain an\n[intent filter](/guide/components/intents-filters). The\n[Activity](/reference/android/app/Activity) that sends work requests to the service uses an\nexplicit [Intent](/reference/android/content/Intent), so no filter is needed. This also\nmeans that only components in the same app or other applications with the\nsame user ID can access the service.\n\n\nNow that you have the basic [IntentService](/reference/android/app/IntentService) class, you can send work requests\nto it with [Intent](/reference/android/content/Intent) objects. The procedure for constructing these objects\nand sending them to your [IntentService](/reference/android/app/IntentService) is described in\n[Send work requests to the background service](/training/run-background-service/send-request)."]]