Swift中的电池完全充电通知

时间:2015-06-27 23:15:48

标签: swift

我正在开发一个应用程序,需要在非活动状态下电池完全充电时通知用户。以下代码只能在应用程序内正常工作。当我打开应用程序并获取UIAlertView时应用程序进入前台时会触发或者背景模式。但是,当应用程序处于非活动状态时,没有太多事情发生。有人帮我解释以下代码中缺少的内容。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    var localNotification = launchOptions?[UIApplicationLaunchOptionsLocalNotificationKey] as! UILocalNotification!

    application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound |
        UIUserNotificationType.Alert | UIUserNotificationType.Badge, categories: nil))


    if (localNotification != nil){
        application.applicationIconBadgeNumber = 0
    }

    return true
}

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
    var state: UIApplicationState = UIApplication.sharedApplication().applicationState
    if state == UIApplicationState.Active
    {

        var alert: UIAlertView = UIAlertView(title: "Fully charged", message: notification.alertBody, delegate: self, cancelButtonTitle: "OK")
        alert.show()
    }
     application.applicationIconBadgeNumber = 0
}


func ViewDidLoad() {
    UIDevice.currentDevice().batteryMonitoringEnabled = true


    NSNotificationCenter.defaultCenter().addObserver(self, selector: "notifyFullyCharged:", name: UIDeviceBatteryLevelDidChangeNotification, object: nil)

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "notifyFullyCharged:", name: UIDeviceBatteryStateDidChangeNotification, object: nil)

}



func notifyFullyCharged() {
    UIDevice.currentDevice().batteryMonitoringEnabled = true
    var batterylevel: Float = UIDevice.currentDevice().batteryLevel
    if UIDevice.currentDevice().batteryLevel==1.0 && UIDevice.currentDevice().batteryState==UIDeviceBatteryState.Charging {
        var localNotification: UILocalNotification = UILocalNotification()
        localNotification.fireDate = nil
        localNotification.alertBody = "Unplug your device. Your battery has been fully charged."
        localNotification.alertAction = "Touch to view"
        localNotification.timeZone = NSTimeZone.defaultTimeZone()
        localNotification.soundName = UILocalNotificationDefaultSoundName
        localNotification.repeatInterval = NSCalendarUnit.CalendarUnitMinute
        localNotification.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
        UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
    }

}

func applicationWillResignActive(application: UIApplication) {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game

    notifyFullyCharged()
    application.applicationIconBadgeNumber = 0

}
func applicationDidEnterBackground(application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

    notifyFullyCharged()
    application.applicationIconBadgeNumber = 0
}

func applicationWillEnterForeground(application: UIApplication) {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.

    notifyFullyCharged()
    application.applicationIconBadgeNumber = 0

}

func applicationDidBecomeActive(application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

    notifyFullyCharged()
    application.applicationIconBadgeNumber = 0

}

func applicationWillTerminate(application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.


}

0 个答案:

没有答案