Skip to content

Instantly share code, notes, and snippets.

@marinat
Created June 19, 2017 20:45
Show Gist options
  • Save marinat/68434e541b10b239d8d9f6c1b875ad2a to your computer and use it in GitHub Desktop.
Save marinat/68434e541b10b239d8d9f6c1b875ad2a to your computer and use it in GitHub Desktop.
val myIntent = Intent(this, TimerReceiver::class.java)
val alarmManager = getSystemService(ALARM_SERVICE) as AlarmManager
val pendingIntent = PendingIntent.getService(this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT)
val calendar = Calendar.getInstance()
calendar.set(Calendar.HOUR_OF_DAY, 23)
calendar.set(Calendar.MINUTE, 29)
calendar.set(Calendar.SECOND, 0)
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 48 * 60 * 60 * 1000, pendingIntent) //set repeating every 24 hours
class TimerReceiver : BroadcastReceiver() {
private var mNm : NotificationManager? = null
private val NOTIFICATION = 3456
override fun onReceive(context: Context?, intent: Intent?) {
Log.d("DEBUG", "timer broadcast received")
val builder = NotificationCompat.Builder(context)
builder.setSmallIcon(R.mipmap.ic_launcher)
builder.setContentTitle("Слоты ждут тебя!")
builder.setContentText("Заходи в однорукого бандита!")
builder.setVibrate(longArrayOf(0, 200, 100, 200))
val notification = builder.build()
mNm = context!!.getSystemService(NOTIFICATION_SERVICE) as NotificationManager?
mNm!!.notify(NOTIFICATION, notification)
}
}
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<receiver
android:name=".TimerReceiver"
android:process=":remote">
</receiver>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment