闹钟基本实现

闹钟基本实现

  • 闹钟所实现的基本功能:定时提醒 

  //定义声音

Oc代码   收藏代码
  1.    CFBundleRef mainBundle;  
  2. mainBundle = CFBundleGetMainBundle ();  
  3.   
  4. // Get the URL to the sound file to play  
  5. soundFileURLRef  =  CFBundleCopyResourceURL (  
  6.                                                 mainBundle,  
  7.                                                 CFSTR ("tap"),  
  8.                                                 CFSTR ("aif"),  
  9.                                                 NULL  
  10.                                                 );  
  11.    //将nsstring转为cfstring  
  12.      
  13.      
  14. // Create a system sound object representing the sound file  
  15. AudioServicesCreateSystemSoundID  
  16.    (  
  17.     soundFileURLRef,  
  18.     &soundFileObject  
  19.     );//声音的绑定(类似数据库时用的数据库指针)  

 利用之前介绍的uidatepicker选取要提醒的时间

 

Oc代码   收藏代码
  1. //计算多少秒后闹钟响应时间    
  2.             int hm=(hs*3600)+(ms*60)-sec;  
  3.             //建立后台消息对象    
  4.             UILocalNotification *notification=[[UILocalNotification alloc] init];  
  5.             if (notification!=nil)   
  6.             {     
  7.                 notification.repeatInterval=NSDayCalendarUnit;  
  8.                 NSDate *now1=[NSDate new];  
  9.                 notification.fireDate=[now1 dateByAddingTimeInterval:hm];//距现在多久后触发代理方法  
  10.                 notification.timeZone=[NSTimeZone defaultTimeZone];  
  11.                 notification.soundName = @"tap.aif";  
  12.                 notification.alertBody = [NSString stringWithFormat:NSLocalizedString(@"你设置的时间是:%i : %i .",nil),htime1 ,mtime1];  
  13.                 [[UIApplication sharedApplication]   scheduleLocalNotification:notification];  
  14.                 [now1 release];  
  15.             }  

点击确定时触发此方法

Oc代码   收藏代码
  1. -(void)notfacation{  
Oc代码   收藏代码
  1.     //获取当前时间  
  2.     sure=YES;  
  3.     NSDate* now = [NSDate date];  
  4.     NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];  
  5.     NSDateComponents *comps = [[NSDateComponents alloc] init];  
  6.     NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |  
  7.     NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;  
  8.     comps = [calendar components:unitFlags fromDate:now];  
  9.     hour = [comps hour];  
  10.     min = [comps minute];  
  11.       
  12.     sec = [comps second];   
  13.       
  14.     htime1=[textField.text intValue];  
  15.     mtime1=[textField1.text intValue];  
  16.       
  17.     hs=htime1-hour;  
  18.     ms=mtime1-min;  
  19.     //设置弹出框提醒用户   
  20.     UIAlertView *at=[[UIAlertView alloc] initWithTitle:@"!"   
  21.                                                message:[NSString stringWithFormat:@"你设置的时间: %i:%i ",htime1,mtime1]  
  22.                                               delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:@"关闭",nil];  
  23.     [at setDelegate:self];  
  24.     [at show];  
  25.     [at release];     
  26. }   

  所设定的时间到了会触发此代理

 

Oc代码   收藏代码
  1. //到时间时触发的代理  
  2. - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification  
  3. {  
  4.     NSLog(@"123123123131231231++++++++++++");  
  5.     AudioServicesPlaySystemSound (self.soundFileObject);  
  6.   
  7.     sure=NO;  
  8.     UIApplicationState state = application.applicationState;  
  9.       
  10.     if (state == UIApplicationStateActive)   
  11.     {  
  12.         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"时间提醒"  
  13.                                                         message:notification.alertBody  
  14.                                                        delegate:self  
  15.                                               cancelButtonTitle:@"确定"  
  16.                                               otherButtonTitles:nil];  
  17.         [alert show];  
  18.         [alert release];  
  19.      }  
  20. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值