소리와 진동 알림, 알림 띄우기 안드로이드 스튜디오

2022. 12. 7. 22:17_Study/AndroidStudio

728x90

10-4 소리와 진동 알림🐇¸.•*¨*•¸.•*¨*•¸.•*¨*•¸.•*¨*•

해당 자료는 강의 학습자료이며, Do it! 깡샘의 안드로이드 앱 프로그래밍 with 코틀린을 참고하였습니다.


소리 알림

안드로이드 시스템은 알림(NOTIFICATION), 알람(ALARM), 벨소리(RINGTONE) 등의 소리를 제공하며 이 소리는
RingtonManager로 얻을 수 있다.

 

 

앱에서 자체 음원을 준비해서 재생하는 방법
- 음원 리소스 디렉터리는 res/raw

val notification: Uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val ringtone = RingtoneManager.getRingtone(applicationContext, notification)
ringtone.play()

음악틀기 dreamer 음원을 사용했다.

val player : MediaPlayer = MediaPlayer.create(this,R.raw.dreamer)
player.start()

seekbar을 이용하여 음악을 재생하는 법 아래 링크 참조

https://dingx2-story.tistory.com/29

 

 

진동 알림

- 매니페스트 파일에 <uses-permission>으로 퍼미션 선언
- 진동은 Vibrator 클래스를 이용
- 31 이전 버전에서는 VIBRATOR_SERVICE로 식별되는 시스템 서비스를 이용
- 31 버전부터는 VIBRATOR_MANAGER_SERVICE로 식별되는 VibratorManager라는 시스템 서비스를 얻고 이 서비스에
서 Vibrator를 이용

 

 

 

시간과 패턴을 지정해 진동 울리기(API 레벨 1부터 제공하는 함수)
- open fun vibrate(milliseconds: Long): Unit
- open fun vibrate(pattern: LongArray!, repeat: Int): Unit
진동의 세기까지 지정해 진동 울리기(API 레벨 26부터 제공하는 함수)
- open fun vibrate(vibe: VibrationEffect!): Unit
- VibrationEffect 객체로는 진동이 울리는 시간 이외에 진동의 세기까지 제어
- open static fun createOneShot(milliseconds: Long, amplitude: Int): VibrationEffect!

 

 

 

10-5 알림 띄우기🐇¸.•*¨*•¸.•*¨*•¸.•*¨*•¸.•*¨*•


알림 채널

상태 바에 앱의 정보를 출력하는 것을 알림notification이라고 한다.

 

 

 

Notification을 만들려면 NotificationCompat.Builder가 필요
- 26 버전 이전까지는 빌더를 만들 때 NotificationChannel 정보가 필요 없었지만
- 26 버전부터는 빌더를 만들 때 NotificationChannel을 만들고 이 채널의 식별값을 빌더의 생성자 매개변수에 지정해
줘야 한다.
- 앱의 알림을 채널로 구분하겠다는 의도

 

 

 

- fun setDescription(description: String!): Unit: 채널의 설명 문자열
- fun setShowBadge(showBadge: Boolean): Unit: 홈 화면의 아이콘에 배지 아이콘 출력 여부
- fun setSound(sound: Uri!, audioAttributes: AudioAttributes!): Unit: 알림음 재생
- fun enableLights(lights: Boolean): Unit: 불빛 표시 여부
- fun setLightColor(argb: Int): Unit: 불빛이 표시된다면 불빛의 색상
- fun enableVibration(vibration: Boolean): Unit: 진동을 울릴지 여부
- fun setVibrationPattern(vibrationPattern: LongArray!): Unit: 진동을 울린다면 진동의 패턴

 

 

 

알림 터치 이벤트

인텐트를 준비한 후 Notification 객체에 담아서 이벤트가 발생할 때 인텐트를 실행해 달라고 시스템에 의뢰
- static fun getActivity(context: Context!, requestCode: Int, intent: Intent!, flags: Int): PendingIntent!
- static fun getBroadcast(context: Context!, requestCode: Int, intent: Intent!, flags: Int): PendingIntent!
- static fun getService(context: Context!, requestCode: Int, intent: Intent, flags: Int): PendingIntent!

 

 

 

액션

액션을 최대 3개까지 추가

 

 

 

원격 입력

- 알림에서 사용자 입력을 직접 받는 기법

 

 

 

알림에 액션을 등록

 

 

 

사용자가 입력한 글을 받을 때 코드

 

 

 

프로그레스
- 진행 상황을 프로그레스에 바로 알려 준다.

 

 

 

알림 스타일
- 큰 이미지 스타일

- 긴 텍스트 스타일

- 상자 스타일

- 메시지 스타일 : Person은 알림에 출력될 한 사람의 정보를 담는 클래스