【问题标题】:Detect battery warning was shown iPhone检测到电池警告显示 iPhone
【发布时间】:2011-03-04 00:58:26
【问题描述】:

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

【问题讨论】:

    标签: iphone power-management


    【解决方案1】:

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

    -(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];
    }
    

    【讨论】:

    • 其实这样不行。问题是我必须从已激活通知中检测到它,这可以通过待机或低电量警报来触发。电池电量可能低于 20%,用户按下待机按钮然后重新激活,我无法判断。除非我想错了……
    • 除非您为越狱设备编写应用程序,否则您的想法是错误的。
    • 或者 QA 错了,哈哈。它不是越狱设备。所以你的意思是它不应该在从低电量警报回来时发出确实变为活动的通知?
    猜你喜欢
    • 1970-01-01
    • 2010-12-08
    • 2011-05-11
    • 1970-01-01
    • 1970-01-01
    • 2011-01-13
    • 1970-01-01
    • 1970-01-01
    • 2016-09-03
    相关资源
    最近更新 更多