zoukankan      html  css  js  c++  java
  • 本地消息使用(UILocalNotification,闹钟功能)

    //时间换成NSString

    - (void)randomPickerView:(DatePickView*)randomPickerView selectDate:(NSDate*)selectDate

    {

        //07:01

    //    NSLog(@"ssss %@", selectDate);

        NSAutoreleasePool *pool = [[NSAutoreleasePoolalloc] init];

        NSCalendar *calendar = [[[NSCalendaralloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];

    NSDateComponents *comps = [[[NSDateComponentsalloc] init] autorelease];

        [calendar setTimeZone:[NSTimeZonetimeZoneWithName:@"GMT"]];

        [comps setTimeZone:[NSTimeZonetimeZoneWithName:@"GMT"]];

    NSInteger unitFlags = NSEraCalendarUnit |

    NSYearCalendarUnit |

    NSMonthCalendarUnit |

    NSDayCalendarUnit |

    NSHourCalendarUnit |

    NSMinuteCalendarUnit |

    NSSecondCalendarUnit |

    NSWeekCalendarUnit |

    NSWeekdayCalendarUnit |

    NSWeekdayOrdinalCalendarUnit |

    NSQuarterCalendarUnit;

    comps = [calendar components:unitFlags fromDate:selectDate];

    int hour = [comps hour];

    int min = [comps minute];

        NSString *tempTimeStr = [NSString stringWithFormat:@"%02d:%02d", hour, min];

    //    self.timeStr = tempTimeStr;

        NSLog(@"time :%@", tempTimeStr);

        [self.recodeSwitchDic setObject:tempTimeStr forKey:kLotteryBetWarnTimeKey]; 

        [pool release];

    }

    //清除本地通知

    - (void)cancelLocalNotification

    {

        NSArray *localNotifications = [[UIApplicationsharedApplication] scheduledLocalNotifications];

        for(UILocalNotification *notification in localNotifications)

        {

            NSLog(@"old localNotification:%@", [notification fireDate]);

            [[UIApplicationsharedApplication] cancelLocalNotification:notification];

        }

    }

    //设置本地通知

    - (void)setLocalNotificationWithWeek:(NSString*)localWeek withContent:(NSString*)content

    {

        NSAutoreleasePool *pool = [[NSAutoreleasePoolalloc] init];

        

        NSMutableDictionary* mutableDic = [[NSUserDefaults standardUserDefaults] objectForKey:kLotteryBetWarnKey];

        if(!mutableDic)

        {

            return;

        }

        NSArray *clockTimeArray = [[mutableDic objectForKey:kLotteryBetWarnTimeKey] componentsSeparatedByString:@":"];

    NSDate *dateNow = [NSDate date];

    NSCalendar *calendar = [[[NSCalendaralloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];

    NSDateComponents *comps = [[[NSDateComponentsalloc] init] autorelease];

        //    [calendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];

        //    [comps setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];

    NSInteger unitFlags = NSEraCalendarUnit |

    NSYearCalendarUnit |

    NSMonthCalendarUnit |

    NSDayCalendarUnit |

    NSHourCalendarUnit |

    NSMinuteCalendarUnit |

    NSSecondCalendarUnit |

    NSWeekCalendarUnit |

    NSWeekdayCalendarUnit |

    NSWeekdayOrdinalCalendarUnit |

    NSQuarterCalendarUnit;

    comps = [calendar components:unitFlags fromDate:dateNow];

    [comps setHour:[[clockTimeArray objectAtIndex:0] intValue]];

    [comps setMinute:[[clockTimeArray objectAtIndex:1] intValue]];

    [comps setSecond:0];

    //------------------------------------------------------------------------

        //    NSString* clockMode = @"一、五";

    Byte weekday = [comps weekday];

    NSArray *array = [localWeek componentsSeparatedByString:@""];

    Byte i = 0;

    Byte j = 0;

    int days = 0;

    inttemp = 0;

    Byte count = [array count];

    Byte clockDays[7];

    NSArray *tempWeekdays = [NSArray arrayWithObjects:@"", @"", @"", @"", @"", @"", @"", nil];

    //查找设定的周期模式

    for (i = 0; i < count; i++) {

    for (j = 0; j < 7; j++) {

    if ([[array objectAtIndex:i] isEqualToString:[tempWeekdays objectAtIndex:j]]) {

    clockDays[i] = j + 1;

    break;

    }

    }

    }

    for (i = 0; i < count; i++) {

        temp = clockDays[i] - weekday;

    days = (temp >= 0 ? temp : temp + 7);

    NSDate *newFireDate = [[calendar dateFromComponents:comps] dateByAddingTimeInterval:3600 * 24 * days];

    UILocalNotification *newNotification = [[UILocalNotificationalloc] init];

    if (newNotification) {

    newNotification.fireDate = newFireDate;

    newNotification.alertBody = content;//设置弹出对话框的消息内容

                if([[mutableDic objectForKey:kLotteryBetWarnSongKey] isEqualToString:@"1"])

                    newNotification.soundName = UILocalNotificationDefaultSoundName;//设置声音

    newNotification.alertAction = @"确定";//设置弹出对话框的按钮

    newNotification.repeatInterval = NSWeekCalendarUnit;//周期为每星期

                newNotification.applicationIconBadgeNumber = 1;//设置BadgeNumber

                

    NSDictionary *userInfo = [NSDictionarydictionaryWithObject:@"1"forKey:@"betLotteryWarnClock"];

    newNotification.userInfo = userInfo;

    [[UIApplicationsharedApplication] scheduleLocalNotification:newNotification];//记录

    }

    NSLog(@"Post new localNotification:%@", [newNotification fireDate]);

    [newNotification release];

    }

        [pool release];

    }

  • 相关阅读:
    perl 模拟curl 发送json数据
    perl put 发送数据
    8小时浓度均值即连续8个小时浓度的平均值
    awk 字段匹配
    rsyslog imfile配置
    EasyUI datetimebox 的onchange事件的问题
    5大领先的商业智能解决方案,国产上榜!
    5大领先的商业智能解决方案,国产上榜!
    perl post 带中文名字的文件
    Openstack 实现技术分解 (2) 虚拟机初始化工具 — Cloud-Init & metadata & userdata
  • 原文地址:https://www.cnblogs.com/swallow37/p/2845761.html
Copyright © 2011-2022 走看看