Я создаю приложение только для самостоятельного использования - на основе восхода и захода солнца устанавливаются разные будильники (время мусульманских намазов)
Для будильника я использовал таймер, который постоянно проверяет время устройства с временем будильника;если оба сигнала одинаковы, сигнал тревоги воспроизводится.
Но я не могу отключить сигнал тревоги после нажатия на уведомление.
onResume
Я пытался mediaplayer.stop()
, но он воспроизводит мультимедиа несколько раз.
void checkToPlaySounds(){
cal = Calendar.getInstance();
format = new SimpleDateFormat("HH:mm");
currentTime = format.format(cal.getTime());
dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
if(mediaPlayer.isPlaying()){
return;
}
if(currentTime.equals(fajr)){
if(chkAzzanFajr.isChecked()){
if(dayOfWeek==1 && chkAzzanSun.isChecked()){playSounds(1);}
if(dayOfWeek==2 && chkAzzanMon.isChecked()){playSounds(1);}
if(dayOfWeek==3 && chkAzzanTue.isChecked()){playSounds(1);}
if(dayOfWeek==4 && chkAzzanWed.isChecked()){playSounds(1);}
if(dayOfWeek==5 && chkAzzanThu.isChecked()){playSounds(1);}
if(dayOfWeek==6 && chkAzzanFri.isChecked()){playSounds(1);}
if(dayOfWeek==7 && chkAzzanSat.isChecked()){playSounds(1);}
}
}
if(currentTime.equals(zohr)){
if(chkAzzanZohr.isChecked()){
if(dayOfWeek==1 && chkAzzanSun.isChecked()){playSounds(1);}
if(dayOfWeek==2 && chkAzzanMon.isChecked()){playSounds(1);}
if(dayOfWeek==3 && chkAzzanTue.isChecked()){playSounds(1);}
if(dayOfWeek==4 && chkAzzanWed.isChecked()){playSounds(1);}
if(dayOfWeek==5 && chkAzzanThu.isChecked()){playSounds(1);}
if(dayOfWeek==6 && chkAzzanFri.isChecked()){playSounds(1);}
if(dayOfWeek==7 && chkAzzanSat.isChecked()){playSounds(1);}
}
}
if(currentTime.equals(magrib)){
if(chkAzzanMaghrib.isChecked()){
if(dayOfWeek==1 && chkAzzanSun.isChecked()){playSounds(1);}
if(dayOfWeek==2 && chkAzzanMon.isChecked()){playSounds(1);}
if(dayOfWeek==3 && chkAzzanTue.isChecked()){playSounds(1);}
if(dayOfWeek==4 && chkAzzanWed.isChecked()){playSounds(1);}
if(dayOfWeek==5 && chkAzzanThu.isChecked()){playSounds(1);}
if(dayOfWeek==6 && chkAzzanFri.isChecked()){playSounds(1);}
if(dayOfWeek==7 && chkAzzanSat.isChecked()){playSounds(1);}
}
}
if(currentTime.equals(fajrReminder) && reminderMinutes!=0){
if(chkAlarmFajr.isChecked()){
if(dayOfWeek==1 && chkAlarmSun.isChecked()){playSounds(0);}
if(dayOfWeek==2 && chkAlarmMon.isChecked()){playSounds(0);}
if(dayOfWeek==3 && chkAlarmTue.isChecked()){playSounds(0);}
if(dayOfWeek==4 && chkAlarmWed.isChecked()){playSounds(0);}
if(dayOfWeek==5 && chkAlarmThu.isChecked()){playSounds(0);}
if(dayOfWeek==6 && chkAlarmFri.isChecked()){playSounds(0);}
if(dayOfWeek==7 && chkAlarmSat.isChecked()){playSounds(0);}
}
}
if(currentTime.equals(zohrReminder) && reminderMinutes!=0){
if(chkAlarmZohr.isChecked()){
if(dayOfWeek==1 && chkAlarmSun.isChecked()){playSounds(0);}
if(dayOfWeek==2 && chkAlarmMon.isChecked()){playSounds(0);}
if(dayOfWeek==3 && chkAlarmTue.isChecked()){playSounds(0);}
if(dayOfWeek==4 && chkAlarmWed.isChecked()){playSounds(0);}
if(dayOfWeek==5 && chkAlarmThu.isChecked()){playSounds(0);}
if(dayOfWeek==6 && chkAlarmFri.isChecked()){playSounds(0);}
if(dayOfWeek==7 && chkAlarmSat.isChecked()){playSounds(0);}
}
}
if(currentTime.equals(magribReminder) && reminderMinutes!=0){
if(chkAlarmMaghrib.isChecked()){
if(dayOfWeek==1 && chkAlarmSun.isChecked()){playSounds(0);}
if(dayOfWeek==2 && chkAlarmMon.isChecked()){playSounds(0);}
if(dayOfWeek==3 && chkAlarmTue.isChecked()){playSounds(0);}
if(dayOfWeek==4 && chkAlarmWed.isChecked()){playSounds(0);}
if(dayOfWeek==5 && chkAlarmThu.isChecked()){playSounds(0);}
if(dayOfWeek==6 && chkAlarmFri.isChecked()){playSounds(0);}
if(dayOfWeek==7 && chkAlarmSat.isChecked()){playSounds(0);}
}
}
if(currentTime.equals(sioriStartReminder) && Integer.valueOf(edtSioriBeforeStart.getText().toString())!=0){
if(dayOfWeek==1 && chkSioriSun.isChecked()){playSounds(0);}
if(dayOfWeek==2 && chkSioriMon.isChecked()){playSounds(0);}
if(dayOfWeek==3 && chkSioriTue.isChecked()){playSounds(0);}
if(dayOfWeek==4 && chkSioriWed.isChecked()){playSounds(0);}
if(dayOfWeek==5 && chkSioriThu.isChecked()){playSounds(0);}
if(dayOfWeek==6 && chkSioriFri.isChecked()){playSounds(0);}
if(dayOfWeek==7 && chkSioriSat.isChecked()){playSounds(0);}
}
if(currentTime.equals(sioriEndReminder) && Integer.valueOf(edtSioriBeforeStart.getText().toString())!=0){
if(dayOfWeek==1 && chkSioriSun.isChecked()){playSounds(0);}
if(dayOfWeek==2 && chkSioriMon.isChecked()){playSounds(0);}
if(dayOfWeek==3 && chkSioriTue.isChecked()){playSounds(0);}
if(dayOfWeek==4 && chkSioriWed.isChecked()){playSounds(0);}
if(dayOfWeek==5 && chkSioriThu.isChecked()){playSounds(0);}
if(dayOfWeek==6 && chkSioriFri.isChecked()){playSounds(0);}
if(dayOfWeek==7 && chkSioriSat.isChecked()){playSounds(0);}
}
}
void playSounds(int i){
String message = "Stop";
//1=Azzan; 0=Alarm
if(i==0){
mediaPlayer= MediaPlayer.create(MainActivity.this,R.raw.alram);
mediaPlayer.setLooping(true);
mediaPlayer.start();
Intent activityIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this,
0, activityIntent, 0);
activityIntent.putExtra("stopMedia",true);
Intent broadcastIntent = new Intent(this, NotificationReceiver.class);
broadcastIntent.putExtra("toastMessage", message);
PendingIntent actionIntent = PendingIntent.getBroadcast(this,
0, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_1_ID)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Mumineen Alarm")
.setContentText("Namaz Reminder - Tap to Stop")
.setSmallIcon(R.drawable.ic_launcher)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_ALARM)
.setColor(Color.BLUE)
.setContentIntent(contentIntent)
.setAutoCancel(true)
.setOnlyAlertOnce(true)
.build();
notificationManager.notify(1, notification);
}
if(i==1){
mediaPlayer= MediaPlayer.create(MainActivity.this,R.raw.azaan);
mediaPlayer.start();
}
}