我是Android开发的新手。
在我的应用程序中,我需要有警报通知。 我有谷歌它,有很多链接但我无法理解在特定时间开始报警通知。
我有五次特别的时间。此时应开始报警声音通知。 警报声是my.mp3,我应该用mp3通知警报。
如果假设,我有时间这样
String time1 = "03:37";
String time2 = "08:45";
String time3 = "13:01";
String time4 = "20:10";
String time5 = "09:08";
我试过这样但没有工作
Date dt = new Date();
int hours = dt.getHours();
int minutes = dt.getMinutes();
int seconds = dt.getSeconds();
StringBuilder sb = new StringBuilder();
if(hours>=12){
sb.append(hours-12).append( ":" ).append(minutes);
}else{
sb.append(hours).append( ":" ).append(minutes);
}
String Timeformat = sb.toString();
curTime = hours+":"+minutes;
if(curTime.equalsIngoreCase(time1){
Intent myIntent = new Intent(DayView.this, MyAlarmService.class);
pendingIntent = PendingIntent.getService(DayView.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}else{
....
}
我该怎么办?非常感谢提前。抱歉我的语法错误。
答案 0 :(得分:0)
添加此代码
if(curTime.equalsIngoreCase(time1)){
Intent myIntent = new Intent(DayView.this, MyAlarmService.class);
pendingIntent = PendingIntent.getService(DayView.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
else if(curTime.equalsIngoreCase(time2)){
Intent myIntent = new Intent(DayView.this, MyAlarmService.class);
pendingIntent = PendingIntent.getService(DayView.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
else if(curTime.equalsIngoreCase(time3)){
Intent myIntent = new Intent(DayView.this, MyAlarmService.class);
pendingIntent = PendingIntent.getService(DayView.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
else if(curTime.equalsIngoreCase(time4)){
Intent myIntent = new Intent(DayView.this, MyAlarmService.class);
pendingIntent = PendingIntent.getService(DayView.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
else if(curTime.equalsIngoreCase(time5)){
Intent myIntent = new Intent(DayView.this, MyAlarmService.class);
pendingIntent = PendingIntent.getService(DayView.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}