-
-
Save AdamMc331/66c302c99f50e464f0f217e29d03231e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
R.id.submit -> if (validInput()) { | |
// TODO: save Alarm | |
val alarm = TextAlarm(alarmName?.text.toString(), alarmTime) | |
var alarmIntent = Intent(this, AlarmReceiver::class.java) | |
val bundle = Bundle() | |
bundle.putParcelable(DismissAlarmActivity.ALARM_EXTRA, alarm) | |
alarmIntent.putExtras(bundle) | |
val pendingIntent = PendingIntent.getBroadcast(this@AddTextAlarmActivity, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT) | |
val alarmManager = getSystemService(ALARM_SERVICE) as AlarmManager | |
alarmManager.set(AlarmManager.RTC_WAKEUP, alarm.time?.asCalendar?.timeInMillis ?: 0, pendingIntent) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.adammcneilly.personalalarmclock | |
import android.content.Context | |
import android.content.Intent | |
import android.media.RingtoneManager | |
import android.net.Uri | |
import android.support.v4.content.WakefulBroadcastReceiver | |
import android.util.Log | |
/** | |
* Called when an alarm goes off. | |
* Created by adam.mcneilly on 3/25/17. | |
*/ | |
class AlarmReceiver : WakefulBroadcastReceiver() { | |
override fun onReceive(context: Context, intent: Intent) { | |
// Get alarm and send to new activity | |
val alarm = intent.extras.getParcelable<Alarm>(DismissAlarmActivity.ALARM_EXTRA) | |
// Only start an alarm if alarm is not null, otherwise we screw over the user | |
if (alarm != null) { | |
val intent = Intent(context, DismissAlarmActivity::class.java) | |
intent.putExtra(DismissAlarmActivity.ALARM_EXTRA, alarm) | |
context.startActivity(intent) | |
//this will sound the alarm tone | |
//this will sound the alarm once, if you wish to | |
//raise alarm in loop continuously then use MediaPlayer and setLooping(true) | |
var alarmUri: Uri? = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM) | |
if (alarmUri == null) { | |
alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION) | |
} | |
val ringtone = RingtoneManager.getRingtone(context, alarmUri) | |
ringtone.play() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment