-
[AOS] ForegroundService2022. 11. 15. 13:21λ°μνπ±μ€μ리λ, μ±μ΄ μ€νλμ΄ μλ λμ μλΉμ€λ κ°μ΄ μ μ§λλλ‘ κ΅¬νν΄μ£ΌμΈμ.(ν.. κ»μ΄μ§γ ) λ΅ ππΆπ±μ€μ리λ... μ±μ΄ λ°±κ·ΈλΌμ΄λλ‘ λ€μ΄κ°λ©΄ μλΉμ€κ° 1λΆ λ€μ μ£½λλ°μ...?μ...βοΈβοΈπΆ
μλΉμ€ (Service)
Declares a service (a Service subclass) as one of the application's components. Unlike activities, services lack a visual user interface. They're used to implement long-running background operations or a rich communications API that can be called by other applications.
All services must be represented by <service> elements in the manifest file. Any that are not declared there will not be seen by the system and will never be run.
A Service is an application component that can perform long-running operations in the background. It does not provide a user interface. Once started, a service might continue running for some time, even after the user switches to another application. Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC). For example, a service can handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background.
μλλ‘μ΄λμ μλΉμ€λ activityμ λ¬λ¦¬ μ¬μ©μ μΈν°νμ΄μ€ μμ΄ λ°±κ·ΈλΌμ΄λμμ μ€νλλ κΈ°λ₯μ λ§νλ€.
μλΉμ€λ AndroidManifest.xml νμΌμ μ μΈ ν μ¬μ©ν μ μλ€.
μλΉμ€μλ μΈ κ°μ§ μ νμ΄ μμΌλ―λ‘ νμν μ νμ μ ν ν ꡬνν΄μΌ νλ€.
ν¬κ·ΈλΌμ΄λ μλΉμ€ (Foreground Service)
ν¬κ·ΈλΌμ΄λ μλΉμ€λ μ¬μ©μμκ² λ³΄μ΄λ μμ μ μννλ€.
μ±κ³Ό μνΈμμ©νμ§ μμ λμλ κ³μ μ€νλλ―λ‘ μ¬μ©μμκ² μν μ€μμ μ리λ μλ¦Όμ νμν΄μΌ νλ€.
ex. μ€λμ€ μ¬μ μ μλ¦Όλ°μ 보μ¬μ§λ νΈλ λ° / λ§λ³΄κΈ° κ±Έμ μ μΈ‘μ μλ¦Ό
λ°±κ·ΈλΌμ΄λ μλΉμ€ (Background Service)
λ°±κ·ΈλΌμ΄λ μλΉμ€λ μ¬μ©μμκ² μ§μ 보μ΄μ§ μλ μμ μ μννλ€. μ¬μ©μκ° μ±κ³Ό μνΈμμ©νμ§ μμΌλ©΄ μλΉμ€ μ€νμ΄ μ νλλ€.
ex. νμΌ λλ κ²μκΈ μ λ‘λ
λ°μΈλ μλΉμ€ (Bind Service)
λ°μΈλ©λ μλΉμ€λ ν΄λΌμ΄μΈνΈ-μλ² μΈν°νμ΄μ€λ₯Ό μ 곡νμ¬ κ΅¬μ± μμκ° μλΉμ€μ μνΈμμ©νκ² νλ©°, κ²°κ³Όλ₯Ό λ°μ μλ μκ³ μ¬μ§μ΄ μ΄μ κ°μ μμ μ μ¬λ¬ νλ‘μΈμ€μ κ±Έμ³ νλ‘μΈμ€ κ° ν΅μ (IPC)μΌλ‘ μνν μλ μλ€.
λ°μΈλ©λ μλΉμ€λ λ λ€λ₯Έ μ ν리μΌμ΄μ κ΅¬μ± μμκ° μ΄μ λ°μΈλ©λμ΄ μλ κ²½μ°μλ§ μ€νλλ€.
μ¬λ¬ κ°μ κ΅¬μ± μμκ° μλΉμ€μ νκΊΌλ²μ λ°μΈλ©λ μ μμ§λ§, μ΄ λͺ¨λ κ²μμ λ°μΈλ©μ΄ ν΄μ λλ©΄ ν΄λΉ μλΉμ€λ μλ©Έλλ€.
μ΄ μ€ μ¬μ©μκ° μ±κ³Ό μνΈμμ©νμ§ μμ λμλ μλΉμ€κ° μ€νλμ΄μΌ νλ―λ‘ Foreground Service ꡬνμ νλ € νλ€.
Foreground Service ꡬννκΈ°
π‘ Android 9 (API 28) λ―Έλ§μ μ±μ startService() λ§μΌλ‘λ μΆ©λΆν Foreground Serviceλ₯Ό ꡬνν μ μλ€.
νμ§λ§ Android 9 μ΄μ λ 벨μ νκΉμΌλ‘ νλ μ±μ notificationμ΄ νμμ μ΄λ©° startForegroundService()λ₯Ό μ΄μ©ν΄ Foreground Serviceλ₯Ό ꡬνν΄μΌ νλ€.1. AndroidManifest.xml νμΌμ permission μ½μ
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
μμ€ν μ΄ μλμΌλ‘ κΆνμ λΆμ¬νκΈ° λλ¬Έμ μΆκ° μ²λ¦¬λ νμνμ§ μλ€.
2. AndroidManifest.xml νμΌμ Service μΆκ°
<service android:name="μλΉμ€λͺ " android:stopWithTask="false" />
3. Service ν΄λμ€ μμ±
class ServiceTask: Service() { override fun onBind(p0: Intent?): IBinder? { return null } override fun onCreate() { super.onCreate() startForegroundService() } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { return START_STICKY } private fun startForegroundService() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val notificationManager = applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager var channel = NotificationChannel("Notification channel id", "Notification channel name", NotificationManager.IMPORTANCE_DEFAULT) channel.setShowBadge(false) notificationManager.createNotificationChannel(channel) var notification: Notification? = NotificationCompat.Builder(this, channel.id).apply { setContentTitle("Foreground Service") setContentText("Foreground Serviceκ° κ΅¬λ μ€μ λλ€.") setSmallIcon(R.drawable.ic_service) }.build() startForeground(NOTIFICATION_ID, notification) } } override fun onTaskRemoved(rootIntent: Intent?) { super.onTaskRemoved(rootIntent) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { stopForeground(true) } stopSelf() } }
startForegroundService() ν¨μμ Notification μμ±μ νμμ μ΄λ€.
λν ν΄λΉ ν¨μ νλ¨μ startForeground() ν¨μλ₯Ό μ€νν΄μ£Όμ§ μμΌλ©΄ μ±μ΄ λ°±κ·ΈλΌμ΄λ μνμΌ λ 1λΆ ν μλΉμ€κ° μλ©Έλλ―λ‘ κΌ μ€νν΄μ£Όμ΄μΌ νλ€.
onStartCommandμ λ°νκ°μ μ’ λ₯λ λ€μκ³Ό κ°λ€.
- START_NOT_STICKY : μλΉμ€ λΉμ μ μ’ λ£ μ μλΉμ€λ₯Ό λͺ μμ μΌλ‘ λ€μ μμν λ κΉμ§ μ€ννμ§ μλλ€.
- START_STICKY : μλΉμ€κ° λΉμ μ μ’ λ£ μ μλΉμ€λ₯Ό λ€μ μ€ννμ§λ§ λ§μ§λ§ Intentλ₯Ό onStartCommandμ μΈμλ‘ λ€μ μ λ¬νμ§ μλλ€. μ΄λ μΌλ¨ μλΉμ€κ° κ³μ μ΄μμμ΄μΌνμ§λ§ λ³λ€λ₯Έ λμμ΄ νμνμ§ μμ μ± μλΉμ€μ μ ν©νλ€.
- START_REDELIVER_INTENT : λ§μ§λ§ Intentλ₯Ό onStartCommandμ μΈμλ‘ λ€μ μ λ¬ν΄μ€λ€. μ¦κ°μ μΈ λ°μμ΄ νμν μλΉμ€μ μ ν©νλ€.
ꡬνν΄μΌνλ μλΉμ€μ λ§μΆμ΄ μ€μ ν΄μ£Όλ©΄ λλ€.
4. Activity μμ Service μ±ν
private lateinit var serviceIntent: Intent override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) serviceIntent = Intent(this, ServiceTask::class.java) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { startForegroundService(serviceIntent) } else { startService(serviceIntent) } ... }
μ±μ΄ μ’ λ£λκΈ° μ κΉμ§ μ’ λ£λμ§ μλ μλΉμ€ ꡬν μ.
λ°μν'π° Android π > π κ°λ°' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[AOS] μ΄λ―Έμ§ λ¬Έμλ©μμ§ μ μ‘νκΈ° (0) 2023.04.14 [AOS] Android DeepLink μ€μ (0) 2022.11.23 [AOS] Android KakaoLink μΉ΄μΉ΄μ€ν‘ 곡μ νκΈ° 2 - Kakao Link μ€μ (0) 2022.11.13 [AOS] Android KakaoLink μΉ΄μΉ΄μ€ν‘ 곡μ νκΈ° 1 - μ΄κΈ° μ€μ (0) 2022.11.13 [AOS] Android Release key hash λ°κΈνκΈ° (0) 2022.11.12