检测 iPhone 显示电池警告

发布于 2024-10-20 03:00:28 字数 115 浏览 4 评论 0原文

有没有办法检测是否显示电池电量警告?我向 UIApplicationDidBecomeActiveNotification 注册了一个通知,我想知道它是否是由于电池电量不足警告而触发的,以便我可以以不同的方式处理它。

Is there a way to detect whether a battery level warning was shown? I registered a notification to UIApplicationDidBecomeActiveNotification and I want to know whether it was triggered due to a low battery warning so I can handle it differently.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

方圜几里 2024-10-27 03:00:28

您可以通过编程方式监控电池电量,当电量达到一定水平时,您可以处理您的事件。

-(NSString*)batteryStateStatus:(UIDeviceBatteryState)state{
    switch ( state )
    {
        case UIDeviceBatteryStateUnknown:
            return @"Unknown";
            break;
        case UIDeviceBatteryStateUnplugged:
            return @"Unplugged";
            break;
        case UIDeviceBatteryStateCharging:
            return @"Charging";
        case UIDeviceBatteryStateFull:
            return @"Charged";
    }

    return nil;
}

-(NSString *)getBatteryPercent
{
    CFTypeRef blob = IOPSCopyPowerSourcesInfo();
    CFArrayRef sources = IOPSCopyPowerSourcesList(blob);

    CFDictionaryRef pSource = NULL;
    const void *psValue;

    NSString *thePercent;

    int i;
    int curCapacity = 0;
    int maxCapacity = 0;
    int percent;

    int numOfSources = CFArrayGetCount(sources);
    //if (numOfSources == 0) return 1;

    for (i = 0 ; i < numOfSources ; i++)
    {
        pSource = IOPSGetPowerSourceDescription(blob, CFArrayGetValueAtIndex(sources, i));
        //if (!pSource) return 2;

        psValue = (CFStringRef)CFDictionaryGetValue(pSource, CFSTR(kIOPSNameKey));

        psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSCurrentCapacityKey));
        CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &curCapacity);

        psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSMaxCapacityKey));
        CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &maxCapacity);

        percent = (int)((double)curCapacity/(double)maxCapacity * 100);
    }

    return [NSString stringWithFormat:@"%d",percent];
}

You can monitor the battery level programmatically, and when it reaches a certain level, you can handle your event then.

-(NSString*)batteryStateStatus:(UIDeviceBatteryState)state{
    switch ( state )
    {
        case UIDeviceBatteryStateUnknown:
            return @"Unknown";
            break;
        case UIDeviceBatteryStateUnplugged:
            return @"Unplugged";
            break;
        case UIDeviceBatteryStateCharging:
            return @"Charging";
        case UIDeviceBatteryStateFull:
            return @"Charged";
    }

    return nil;
}

-(NSString *)getBatteryPercent
{
    CFTypeRef blob = IOPSCopyPowerSourcesInfo();
    CFArrayRef sources = IOPSCopyPowerSourcesList(blob);

    CFDictionaryRef pSource = NULL;
    const void *psValue;

    NSString *thePercent;

    int i;
    int curCapacity = 0;
    int maxCapacity = 0;
    int percent;

    int numOfSources = CFArrayGetCount(sources);
    //if (numOfSources == 0) return 1;

    for (i = 0 ; i < numOfSources ; i++)
    {
        pSource = IOPSGetPowerSourceDescription(blob, CFArrayGetValueAtIndex(sources, i));
        //if (!pSource) return 2;

        psValue = (CFStringRef)CFDictionaryGetValue(pSource, CFSTR(kIOPSNameKey));

        psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSCurrentCapacityKey));
        CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &curCapacity);

        psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSMaxCapacityKey));
        CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &maxCapacity);

        percent = (int)((double)curCapacity/(double)maxCapacity * 100);
    }

    return [NSString stringWithFormat:@"%d",percent];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文