2014-04-30 222 views
0

我不熟悉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

你讀過android培訓了嗎? http://developer.android.com/training/scheduling/alarms.html – Smokez

+0

@StefanBeike我試着像上面的代碼,但它不工作 – user3588683

回答

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); 
}