Skip to content

Instantly share code, notes, and snippets.

@roberto-o-r
Created October 29, 2018 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roberto-o-r/4a8008ce80be27e9f2c5e986cb2e86f8 to your computer and use it in GitHub Desktop.
Save roberto-o-r/4a8008ce80be27e9f2c5e986cb2e86f8 to your computer and use it in GitHub Desktop.
package com.isscroberto.powernap.util
import android.app.Service
import android.content.Context
import android.content.Intent
import android.media.Ringtone
import android.media.RingtoneManager
import android.os.IBinder
class AlarmService : Service() {
private lateinit var ringtone: Ringtone
override fun onBind(p0: Intent?): IBinder? {
return null
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
playAlarm()
return super.onStartCommand(intent, flags, startId)
}
override fun onDestroy() {
super.onDestroy()
ringtone.stop()
}
fun playAlarm() {
val notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM)
ringtone = RingtoneManager.getRingtone(this, notification)
if(!ringtone.isPlaying()) {
ringtone.play()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment