AlarmManager | XML

FirstFragment.kt

import android.app.AlarmManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.icu.util.Calendar
import android.os.Bundle
import android.os.SystemClock
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.fragment.findNavController
import com.neco_desarrollo.alarmmanager.databinding.FragmentFirstBinding

class FirstFragment : Fragment() {
private val calendar = Calendar.getInstance()
private lateinit var alarmIntent: PendingIntent
private var alarmManager: AlarmManager? = null
private var _binding: FragmentFirstBinding? = null
private val binding get() = _binding!!

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {

_binding = FragmentFirstBinding.inflate(inflater, container, false)
return binding.root

}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.timePicker.setIs24HourView(true)
alarmManager = requireActivity().getSystemService(Context.ALARM_SERVICE) as AlarmManager
alarmIntent = Intent(context, AlarmReceiver::class.java).let { intent ->
intent.putExtra("key", "Hello!")
PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_IMMUTABLE)
}

binding.button.setOnClickListener {
calendar.set(Calendar.HOUR_OF_DAY, binding.timePicker.hour)
calendar.set(Calendar.MINUTE, binding.timePicker.minute)
alarmManager?.setExact(
AlarmManager.RTC_WAKEUP,
calendar.timeInMillis,
alarmIntent
)
}
binding.button2.setOnClickListener {
alarmManager?.setInexactRepeating(
AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime(),
60 * 1000,
alarmIntent
)
}
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}

AlarmReceiver.kt

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log

class AlarmReceiver : BroadcastReceiver() {

override fun onReceive(context: Context, intent: Intent) {
val message = intent.getStringExtra("key")
Log.d("MyLog", "Alarm message: $message")
}
}

разрешение:

<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />

2 комментария для “AlarmManager | XML”

  1. У кого код горит красным после добавления

    “`
    alarmManager?.setExact(
    AlarmManager.RTC_WAKEUP,
    calendar.timeInMillis,
    alarmIntent
    )
    “`
    помогла добавит еще и эту строчку

Добавить комментарий

Ваш адрес email не будет опубликован.