[Android] Service 2편 (Foreground)

2020. 6. 22. 16:35·Android

개요

  • System에 의해 중단될 염려 X
  • 상태 표시줄에 알림을 제공해야함.
  • 권한 필요 (자동 부여) - android.permission.FOREGROUND_SERVICE

실행 요청 및 알림 == startForeground() / 중단 == stopForeground()

 

 

ex) Music Player

 

 

1편에서 살펴봤던 예제는 모두 앱을 종료시키면  (뒤로가기 2회클릭시) 음악이 꺼졌다.

 

[Android] Service <-> Activity Communication (feat. Bind Service)

Service는 App의 Background Thread로 동작한다고 소개했다. activity에서 어떤 버튼을 눌러 Service에게 Intent를 매개로 데이터를 넘겨주는 것(Activity -> Service)은 쉬웠다. 그럼 반대로 Service ->Activity..

m-falcon.tistory.com

 

Foreground Service는 앱을 종료해도 Background에 살아있다.

 

 

사용 방법

 

1. AndroidManifest에 포그라운드 권한등록

android.permission.FOREGROUD_SERVICE

2. notification 객체, 빌더 생성

Foreground Service는 Notification이 필수이다.

아래와 같은 형태로 notification 객체를 생성한다.

       val notificationId = 1000
       val channel = NotificationChannel("channelMP3", "MusicPlayer", NotificationManager.IMPORTANCE_DEFAULT)
        manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        manager?.createNotificationChannel(channel)

        val builder = NotificationCompat.Builder(this, "channelMP3")
            .setSmallIcon(R.drawable.ic_baseline_music_video_24)
            .setContentTitle("MP3")
            .setContentText("Music Playing~~")

        val notification = builder.build()

 

3. Foreground 등록

startForeground 메소드로 등록

        startForeground(notificationId, notification)

4. 등록 해제

stopForeground(true) -> Boolean parameter는 notification 제거 여부를 의미한다.

 

저작자표시 (새창열림)

'Android' 카테고리의 다른 글

[Android] SharedPreference  (0) 2020.07.03
[Android] instance  (0) 2020.07.03
[Android] Service <-> Activity Communication (feat. Bind Service)  (0) 2020.06.21
[Android] Service (feat. Thread)  (0) 2020.06.20
[Android] drawable resize, customizing  (0) 2020.06.20
'Android' 카테고리의 다른 글
  • [Android] SharedPreference
  • [Android] instance
  • [Android] Service <-> Activity Communication (feat. Bind Service)
  • [Android] Service (feat. Thread)
M_Falcon
M_Falcon
  • M_Falcon
    Falcon
    M_Falcon
  • 전체
    오늘
    어제
    • 분류 전체보기 (432) N
      • Web (16)
        • Nodejs (14)
        • Javascript (23)
        • FrontEnd (4)
      • DataBase (39)
        • Fundamental (1)
        • Redis (4)
        • PostgreSQL (10)
        • NoSQL (4)
        • MySQL (9)
        • MSSQL (3)
        • Error (4)
      • Algorithm (79)
        • Algorithm (문제풀이) (56)
        • Algorithm (이론) (23)
      • JVM (65)
        • Spring (13)
        • JPA (5)
        • Kotlin (13)
        • Java (24)
        • Error (7)
      • 기타 (70) N
        • Kafka (3)
        • Kubernetes (3)
        • Docker (13) N
        • git (19)
        • 잡동사니 (27)
      • 재테크 (11)
        • 세무 (4)
        • 투자 (3)
        • 보험 (0)
      • BlockChain (2)
        • BitCoin (0)
      • C (32)
        • C (10)
        • C++ (17)
        • Error (3)
      • Low Level (8)
        • OS (3)
        • 시스템 보안 (5)
      • 네트워크 (3)
      • LINUX (30)
        • Linux (26)
        • Error (4)
      • 저작권과 스마트폰의 이해 (0)
      • 생각 뭉치 (6)
      • 궁금증 (2)
      • Private (4)
        • 이직 경험 (0)
        • 꿈을 찾아서 (1)
      • Android (21)
        • OS (4)
  • 블로그 메뉴

    • 홈
    • WEB
    • 알고리즘
    • DataBase
    • Linux
    • Mobile
    • C
    • 방명록
  • 링크

    • github
  • 공지사항

  • 인기 글

  • 태그

    알고리즘
    javascript
    Spring
    PostgreSQL
    C++
    docker
    Kotlin
    ubuntu
    프로그래머스
    Bitcoin
    android
    algorithm
    java
    백준
    kafka
    database
    Git
    linux
    JPA
    Programmers
  • hELLO· Designed By정상우.v4.10.3
M_Falcon
[Android] Service 2편 (Foreground)
상단으로

티스토리툴바