소리 알림
- 안드로이드 시스템은 알림Notification, 알람Alarm, 벨소리Ringtone 등의 소리를 제공하며 RingtoneManager를 통해 얻을 수 있다.
val notification: Uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val ringtone = RingtoneManager.getRingtone(applicationContext, notification)
ringtone.play()
- 자체 녹음한 음원을 사용할 경우 res/raw 폴더에 넣고 MediaPlayer를 통해 재생한다.
val player: MediaPlayer = MediaPlayer.create(this, R.raw.fallbackring)
player.start()
진동 알림
<uses-permission android:name="android.permission.VIBRATE"/>
- API 31 을 기준으로 VIBRATOR_MANAGER_SERVICE 를 통해서 Vibrator를 가져와야 한다.
val vibrator = if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.S){
val vibratorManager = this.getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager
vibratorManager.defaultVibrator
}else{
getSystemService(VIBRATOR_SERVICE) as Vibrator
}
진동 울리기
- API 레벨 26 버전을 기준으로 VibrationEffect 객체를 사용하여 진동의 세기까지 조절할 수 있도록 변경되었다. ( 기존 vibrate 함수는 Deprecated )
- 기본 1회 진동
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
vibrator.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE))
}else{
vibrator.vibrate(500)
}
- createWaveform을 통한 다양한 진동세기 조절
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
vibrator.vibrate(VibrationEffect.createWaveform(longArrayOf(500,1000,500,2000), intArrayOf(0,50,0,200), -1))
}else{
vibrator.vibrate(longArrayOf(500,1000,500,2000), -1)
}