Alarm Manager Example

前端 未结 10 1384
情话喂你
情话喂你 2020-11-21 05:12

I want to implement a schedule function in my project. So I Googled for an Alarm manager program but I can`t find any examples.

Can anyone help me with a basic alar

10条回答
  •  情歌与酒
    2020-11-21 05:52

    Here's an example with Alarm Manager using Kotlin:

    class MainActivity : AppCompatActivity() {
    
        val editText: EditText by bindView(R.id.edit_text)
        val timePicker: TimePicker by bindView(R.id.time_picker)
        val buttonSet: Button by bindView(R.id.button_set)
        val buttonCancel: Button by bindView(R.id.button_cancel)
        val relativeLayout: RelativeLayout by bindView(R.id.activity_main)
        var notificationId = 0
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            timePicker.setIs24HourView(true)
    
            val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
    
            buttonSet.setOnClickListener {
                if (editText.text.isBlank()) {
                    Toast.makeText(applicationContext, "Title is Required!!", Toast.LENGTH_SHORT).show()
                    return@setOnClickListener
                }
                alarmManager.set(
                    AlarmManager.RTC_WAKEUP,
                    Calendar.getInstance().apply {
                        set(Calendar.HOUR_OF_DAY, timePicker.hour)
                        set(Calendar.MINUTE, timePicker.minute)
                        set(Calendar.SECOND, 0)
                    }.timeInMillis,
                    PendingIntent.getBroadcast(
                        applicationContext,
                        0,
                        Intent(applicationContext, AlarmBroadcastReceiver::class.java).apply {
                            putExtra("notificationId", ++notificationId)
                            putExtra("reminder", editText.text)
                        },
                        PendingIntent.FLAG_CANCEL_CURRENT
                    )
                )
                Toast.makeText(applicationContext, "SET!! ${editText.text}", Toast.LENGTH_SHORT).show()
                reset()
            }
    
            buttonCancel.setOnClickListener {
                alarmManager.cancel(
                    PendingIntent.getBroadcast(
                        applicationContext, 0, Intent(applicationContext, AlarmBroadcastReceiver::class.java), 0))
                Toast.makeText(applicationContext, "CANCEL!!", Toast.LENGTH_SHORT).show()
            }
        }
    
        override fun onTouchEvent(event: MotionEvent?): Boolean {
            (getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager)
                .hideSoftInputFromWindow(relativeLayout.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
            relativeLayout.requestFocus()
            return super.onTouchEvent(event)
        }
    
        override fun onResume() {
            super.onResume()
            reset()
        }
    
        private fun reset() {
            timePicker.apply {
                val now = Calendar.getInstance()
                hour = now.get(Calendar.HOUR_OF_DAY)
                minute = now.get(Calendar.MINUTE)
            }
            editText.setText("")
        }
    }
    

提交回复
热议问题