2009-05-08 10 views
23

चार्ज हो रहा है, तो मैं कुछ भी निश्चित my favourite tool का उपयोग नहीं कर पा सकते हैं, फिर भी मैंने सोचा कि मैं इसे बाहर यहाँ रखा जाएगा ...एक तरह से पता लगाने डिवाइस

वहाँ है, iPhone SDK का उपयोग कर, अगर पता लगाने के लिए एक अनुप्रयोग के लिए डिवाइस बिजली प्राप्त करने की स्थिति में है (चार्जिंग, डॉक, इत्यादि)?

यदि डिवाइस पावर प्राप्त कर रहा है तो मैं स्वचालित रूप से idleTimer को अक्षम करने में सक्षम होना चाहता हूं (अन्यथा यह उपयोगकर्ता द्वारा निर्दिष्ट सेटिंग है)।

+0

Google को जोड़ने पर अच्छा स्पर्श, मैं हँसे। अच्छा सवाल, मुझे समय बचाया। आप और योगदानकर्ताओं के लिए धन्यवाद। –

उत्तर

32

हाँ, UIDevice आप इस कह रही करने में सक्षम है:

[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES]; 

if ([[UIDevice currentDevice] batteryState] == UIDeviceBatteryStateCharging) { 
    NSLog(@"Device is charging."); 
} 

अधिक जानकारी के लिए डॉक्स में UIDevice संदर्भ देखें, और batteryState के अन्य मूल्यों के लिए।

39

आप का उपयोग कर बेहतर कर रहे हैं:

[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES]; 

if ([[UIDevice currentDevice] batteryState] != UIDeviceBatteryStateUnplugged) { 
     [UIApplication sharedApplication].idleTimerDisabled=YES; 
    } 

इसका कारण यह है कि आप दो विभिन्न राज्यों के साथ खुद को चिंता का विषय है - एक है कि बैटरी अन्य चार्ज हो रही है, और जब यह है पूरी तरह चार्ज

आप वास्तव में इसके बारे में पूरा होना चाहते थे - आप बैटरी निगरानी सूचनाएँ प्राप्त करने के, तो आप सकता निष्क्रिय टाइमर यदि उपयोगकर्ता मुख्य विद्युत काट दिया पुन: सक्षम, आदि आप उपयोग कर सकते हैं रजिस्टर होगा

0

darwin notification center और घटना का नाम com.apple.springboard.fullycharged का उपयोग करें।

// Registering for a specific notification 
NSString *notificationName = @"com.apple.springboard.fullycharged"; 
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), 
           NULL, 
           yourCustomMethod, 
           (__bridge CFStringRef)notificationName, 
           NULL, 
           CFNotificationSuspensionBehaviorDeliverImmediately); 

// The custom method that will receive the notification 
static void yourCustomMethod(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) 
{ 
    NSString *nameOfNotification = (__bridge NSString*)name; 

    if([nameOfNotification isEqualToString:notificationName]) 
    { 
    // Do whatever you want... 
    } 
} 
+1

उत्तर आईओएस है ... आपको स्निप कोड आज़माएं। मैंने इसे अपनी कुछ परियोजनाओं में इस्तेमाल किया। इसके अलावा, उपरोक्त हाइपरलिंक पढ़ें। – OhadM

4

स्विफ्ट

UIDevice.currentDevice().batteryMonitoringEnabled = true; 

if (UIDevice.currentDevice().batteryState != .Unplugged) { 
    print("Device is charging."); 
} 
4

स्विफ्ट 3::

इस तरह आप अपने कस्टम विधि के लिए एक सूचना प्राप्त होगी, यहाँ एक कोड स्निप है

UIDevice.current.isBatteryMonitoringEnabled = true 
let state = UIDevice.current.batteryState 

if state == .charging || state == .full { 
    print("Device plugged in.") 
} 
+0

हो सकता है, यह "राज्य = UIDevice.current.batteryState" –

+1

@ बाटरोबुई और अद्यतन करने का सुझाव देने वाले सभी को हो; धन्यवाद – UpSampler

संबंधित मुद्दे