Я пишу приложение, о котором нужно оповещать каждый день (по выбору) в установленное время. Всякий раз, когда я выбираю текущий день, будильник срабатывает хорошо, но когда я устанавливаю будильник на другой день, в этот момент всплывает будильник. Спасибо за ваше время и ваши усилия !!
это моя деятельность:
class setTimeActivity1 : AppCompatActivity() {
val uid = FirebaseAuth.getInstance().uid
private var cal = Calendar.getInstance()
lateinit var context:Context
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_set_time)
enableNotification.isChecked = false
importData()
context = this
val myAlarmManagerMon = getSystemService(Context.ALARM_SERVICE)as AlarmManager
val myAlarmManagerTue = getSystemService(Context.ALARM_SERVICE)as AlarmManager
val myAlarmManagerWed = getSystemService(Context.ALARM_SERVICE)as AlarmManager
val myAlarmManagerThu = getSystemService(Context.ALARM_SERVICE)as AlarmManager
val myAlarmManagerFri = getSystemService(Context.ALARM_SERVICE)as AlarmManager
val myAlarmManagerSat = getSystemService(Context.ALARM_SERVICE)as AlarmManager
val myAlarmManagerSun = getSystemService(Context.ALARM_SERVICE)as AlarmManager
var myPendingIntentMon : PendingIntent?
var myPendingIntentTue : PendingIntent?
var myPendingIntentWed : PendingIntent?
var myPendingIntentThu : PendingIntent?
var myPendingIntentFri : PendingIntent?
var myPendingIntentSat : PendingIntent?
var myPendingIntentSun : PendingIntent?
buttonMon.setOnClickListener {
if (buttonMon.isSelected == true){
buttonMon.isSelected = false
buttonMon.setBackgroundColor(Color.parseColor("#C7C6C6"))
buttonMon.setTypeface(null,Typeface.NORMAL)
buttonMon.textSize = 12.0f
}else{
buttonMon.isSelected = true
buttonMon.setBackgroundColor(Color.WHITE)
buttonMon.setTypeface(null,Typeface.BOLD)
buttonMon.textSize = 13.0f
}
}
buttonTue.setOnClickListener {
if (buttonTue.isSelected == true){
buttonTue.isSelected = false
buttonTue.setBackgroundColor(Color.parseColor("#C7C6C6"))
buttonTue.setTypeface(null,Typeface.NORMAL)
buttonTue.textSize = 12.0f
}else{
buttonTue.isSelected = true
buttonTue.setBackgroundColor(Color.WHITE)
buttonTue.setTypeface(null,Typeface.BOLD)
buttonTue.textSize = 13.0f
}
}
buttonWed.setOnClickListener {
if (buttonWed.isSelected == true){
buttonWed.isSelected = false
buttonWed.setBackgroundColor(Color.parseColor("#C7C6C6"))
buttonWed.setTypeface(null,Typeface.NORMAL)
buttonWed.textSize = 12.0f
}else{
buttonWed.isSelected = true
buttonWed.setBackgroundColor(Color.WHITE)
buttonWed.setTypeface(null,Typeface.BOLD)
buttonWed.textSize = 13.0f
}
}
buttonThu.setOnClickListener {
if (buttonThu.isSelected == true){
buttonThu.isSelected = false
buttonThu.setBackgroundColor(Color.parseColor("#C7C6C6"))
buttonThu.setTypeface(null,Typeface.NORMAL)
buttonThu.textSize = 12.0f
}else{
buttonThu.isSelected = true
buttonThu.setBackgroundColor(Color.WHITE)
buttonThu.setTypeface(null,Typeface.BOLD)
buttonThu.textSize = 13.0f
}
}
buttonFri.setOnClickListener {
if (buttonFri.isSelected == true){
buttonFri.isSelected = false
buttonFri.setBackgroundColor(Color.parseColor("#C7C6C6"))
buttonFri.setTypeface(null,Typeface.NORMAL)
buttonFri.textSize = 12.0f
}else{
buttonFri.isSelected = true
buttonFri.setBackgroundColor(Color.WHITE)
buttonFri.setTypeface(null,Typeface.BOLD)
buttonFri.textSize = 13.0f
}
}
buttonSat.setOnClickListener {
if (buttonSat.isSelected == true){
buttonSat.isSelected = false
buttonSat.setBackgroundColor(Color.parseColor("#C7C6C6"))
buttonSat.setTypeface(null,Typeface.NORMAL)
buttonSat.textSize = 12.0f
}else{
buttonSat.isSelected = true
buttonSat.setBackgroundColor(Color.WHITE)
buttonSat.setTypeface(null,Typeface.BOLD)
buttonSat.textSize = 13.0f
}
}
buttonSun.setOnClickListener {
if (buttonSun.isSelected == true){
buttonSun.isSelected = false
buttonSun.setBackgroundColor(Color.parseColor("#C7C6C6"))
buttonSun.setTypeface(null,Typeface.NORMAL)
buttonSun.textSize = 12.0f
}else{
buttonSun.isSelected = true
buttonSun.setBackgroundColor(Color.WHITE)
buttonSun.setTypeface(null,Typeface.BOLD)
buttonSun.textSize = 13.0f
}
}
continueToMainScrean.setOnClickListener {
if (enableNotification.isChecked == false) {
val leagueIntent = Intent(this, homeActivity::class.java)
startActivity(leagueIntent)
} else {
if (setTimeText.text.count() > 0) {
if (buttonMon.isSelected == true) {
val setTimeMon = Calendar.getInstance()
val dateTime = setTimeText.text.split(":")
setTimeMon.set(Calendar.HOUR_OF_DAY, dateTime[1].toInt())
setTimeMon.set(Calendar.MINUTE, dateTime[2].toInt())
setTimeMon.set(Calendar.SECOND, 0)
setTimeMon.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY)
val sendIntent = Intent(this, Reciver::class.java)
sendIntent.putExtra("buttonMon", "buttonMon")
myPendingIntentMon = PendingIntent.getBroadcast(
context,
0,
sendIntent,
PendingIntent.FLAG_UPDATE_CURRENT
)
myAlarmManagerMon.set(
AlarmManager.RTC_WAKEUP,
setTimeMon.timeInMillis,
myPendingIntentMon
)
myAlarmManagerMon.setInexactRepeating(
AlarmManager.RTC_WAKEUP,
setTimeMon.timeInMillis,
AlarmManager.INTERVAL_DAY,
myPendingIntentMon
)
} else {
}
if (buttonTue.isSelected == true) {
val setTimeTue = Calendar.getInstance()
val dateTime = setTimeText.text.split(":")
setTimeTue.set(Calendar.HOUR_OF_DAY, dateTime[1].toInt())
setTimeTue.set(Calendar.MINUTE, dateTime[2].toInt())
setTimeTue.set(Calendar.SECOND, 0)
setTimeTue.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY)
val sendIntent = Intent(this, Reciver::class.java)
sendIntent.putExtra("buttonTue", "buttonTue")
myPendingIntentTue = PendingIntent.getBroadcast(
context,
1,
sendIntent,
PendingIntent.FLAG_UPDATE_CURRENT
)
myAlarmManagerTue.set(
AlarmManager.RTC_WAKEUP,
setTimeTue.timeInMillis,
myPendingIntentTue
)
myAlarmManagerTue.setInexactRepeating(
AlarmManager.RTC_WAKEUP,
setTimeTue.timeInMillis,
AlarmManager.INTERVAL_DAY,
myPendingIntentTue
)
} else {
}
if (buttonWed.isSelected == true) {
val setTimeWed = Calendar.getInstance()
val dateTime = setTimeText.text.split(":")
setTimeWed.set(Calendar.HOUR_OF_DAY, dateTime[1].toInt())
setTimeWed.set(Calendar.MINUTE, dateTime[2].toInt())
setTimeWed.set(Calendar.SECOND, 0)
setTimeWed.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY)
val sendIntent = Intent(this, Reciver::class.java)
sendIntent.putExtra("buttonWed", "buttonWed")
myPendingIntentWed = PendingIntent.getBroadcast(
context,
2,
sendIntent,
PendingIntent.FLAG_UPDATE_CURRENT
)
myAlarmManagerWed.set(
AlarmManager.RTC_WAKEUP,
setTimeWed.timeInMillis,
myPendingIntentWed
)
myAlarmManagerWed.setInexactRepeating(
AlarmManager.RTC_WAKEUP,
setTimeWed.timeInMillis,
AlarmManager.INTERVAL_DAY,
myPendingIntentWed
)
} else {
}
if (buttonThu.isSelected == true) {
val setTimeThu = Calendar.getInstance()
val dateTime = setTimeText.text.split(":")
setTimeThu.set(Calendar.HOUR_OF_DAY, dateTime[1].toInt())
setTimeThu.set(Calendar.MINUTE, dateTime[2].toInt())
setTimeThu.set(Calendar.SECOND, 0)
setTimeThu.set(Calendar.DAY_OF_WEEK, 4)
val sendIntent = Intent(this, Reciver::class.java)
sendIntent.putExtra("buttonThu", "buttonThu")
myPendingIntentThu = PendingIntent.getBroadcast(
context,
3,
sendIntent,
PendingIntent.FLAG_UPDATE_CURRENT
)
myAlarmManagerThu.set(
AlarmManager.RTC_WAKEUP,
setTimeThu.timeInMillis,
myPendingIntentThu
)
myAlarmManagerThu.setInexactRepeating(
AlarmManager.RTC_WAKEUP,
setTimeThu.timeInMillis,
AlarmManager.INTERVAL_DAY,
myPendingIntentThu
)
} else {
}
if (buttonFri.isSelected == true) {
val setTimeFir = Calendar.getInstance()
val dateTime = setTimeText.text.split(":")
setTimeFir.set(Calendar.HOUR_OF_DAY, dateTime[1].toInt())
setTimeFir.set(Calendar.MINUTE, dateTime[2].toInt())
setTimeFir.set(Calendar.SECOND, 0)
setTimeFir.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY)
val sendIntent = Intent(this, Reciver::class.java)
sendIntent.putExtra("buttonFri", "buttonFri")
myPendingIntentFri = PendingIntent.getBroadcast(
context,
4,
sendIntent,
PendingIntent.FLAG_UPDATE_CURRENT
)
myAlarmManagerFri.set(
AlarmManager.RTC_WAKEUP,
setTimeFir.timeInMillis,
myPendingIntentFri
)
myAlarmManagerFri.setInexactRepeating(
AlarmManager.RTC_WAKEUP,
setTimeFir.timeInMillis,
AlarmManager.INTERVAL_DAY,
myPendingIntentFri
)
} else {
}
if (buttonSat.isSelected == true) {
val setTimeSat = Calendar.getInstance()
val dateTime = setTimeText.text.split(":")
setTimeSat.set(Calendar.HOUR_OF_DAY, dateTime[1].toInt())
setTimeSat.set(Calendar.MINUTE, dateTime[2].toInt())
setTimeSat.set(Calendar.SECOND, 0)
setTimeSat.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY)
val sendIntent = Intent(this, Reciver::class.java)
sendIntent.putExtra("buttonSat", "buttonSat")
myPendingIntentSat = PendingIntent.getBroadcast(
context,
5,
sendIntent,
PendingIntent.FLAG_UPDATE_CURRENT
)
myAlarmManagerSat.set(
AlarmManager.RTC_WAKEUP,
setTimeSat.timeInMillis,
myPendingIntentSat
)
myAlarmManagerSat.setInexactRepeating(
AlarmManager.RTC_WAKEUP,
setTimeSat.timeInMillis,
AlarmManager.INTERVAL_DAY,
myPendingIntentSat
)
} else {
}
if (buttonSun.isSelected == true) {
val setTimeSun = Calendar.getInstance()
val dateTime = setTimeText.text.split(":")
setTimeSun.set(Calendar.HOUR_OF_DAY, dateTime[1].toInt())
setTimeSun.set(Calendar.MINUTE, dateTime[2].toInt())
setTimeSun.set(Calendar.SECOND, 0)
setTimeSun.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY)
val sendIntent = Intent(this, Reciver::class.java)
sendIntent.putExtra("buttonSun", "buttonSun")
myPendingIntentSun = PendingIntent.getBroadcast(
context,
6,
sendIntent,
PendingIntent.FLAG_UPDATE_CURRENT
)
myAlarmManagerSun.set(
AlarmManager.RTC_WAKEUP,
setTimeSun.timeInMillis,
myPendingIntentSun
)
myAlarmManagerSun.setInexactRepeating(
AlarmManager.RTC_WAKEUP,
setTimeSun.timeInMillis,
AlarmManager.INTERVAL_DAY,
myPendingIntentSun
)
} else {
}
val leagueIntent = Intent(this, homeActivity::class.java)
startActivity(leagueIntent)
} else {
val leagueIntent = Intent(this, homeActivity::class.java)
startActivity(leagueIntent)
}
}
}
setAlarmButton.setOnClickListener {
TimePickerDialog(
this, myTimePicker(),
cal.get(Calendar.HOUR_OF_DAY),
cal.get(Calendar.MINUTE),
true
).show()
}
}
@SuppressLint("SimpleDateFormat")
private fun setMyTimeFormat():String{
val myFormat = "HH:mm"
val sdf = SimpleDateFormat(myFormat)
return sdf.format(cal.getTime())
}
private fun myTimePicker():TimePickerDialog.OnTimeSetListener{
val timeSetListener = object : TimePickerDialog.OnTimeSetListener{
@SuppressLint("SetTextI18n")
override fun onTimeSet(p0: TimePicker?, hours: Int, minute: Int) {
cal.set(Calendar.HOUR_OF_DAY,hours)
cal.set(Calendar.MINUTE,minute)
setTimeText.text = "notification set to :${setMyTimeFormat()}"
}
}
return timeSetListener
}
private fun importData(){
}
}
это мой менеджер тревог:
class AlarmServis:Service() {
override fun onBind(intent: Intent?): IBinder? {
return null
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
createNotification()
return Service.START_STICKY
}
fun createNotification(){
val notificationManager:NotificationManager
val notificationChannel : NotificationChannel
val builder:Notification.Builder
val channelId = "com.optiwork.myapplication3.controler"
val description = "text notification"
notificationManager = getSystemService(Context.NOTIFICATION_SERVICE)as NotificationManager
val intent = Intent(this,moodRateActivity::class.java)
val pendingIntent = PendingIntent.getActivity(this,0,intent, PendingIntent.FLAG_UPDATE_CURRENT)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationChannel = NotificationChannel(channelId,description,NotificationManager.IMPORTANCE_HIGH)
notificationChannel.enableLights(true)
notificationChannel.lightColor = Color.GREEN
notificationChannel.enableVibration(true)
notificationManager.createNotificationChannel(notificationChannel)
builder = Notification.Builder(this,channelId)
.setContentTitle("How was your work day?")
.setContentText("Rate how was your work day to compare to people like you...")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent)
notificationManager.notify(1234,builder.build())
}
}
}
это мой получатель:
class Reciver: BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val sendIntent = Intent(context,AlarmServis::class.java)
context!!.startService(sendIntent)
}
}