球风无痕
球风无痕
  • 发布:2016-01-12 16:11
  • 更新:2016-07-26 15:20
  • 阅读:1716

有谁做过获得手机电量的例子吗?安卓的弄出来了 IOS的谁帮忙提供下谢谢

分类:Native.js

IOS的获取电量代码如下:

    • (int)getCurrentBatteryLevel
      {
      if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive||[UIApplication sharedApplication].applicationState==UIApplicationStateInactive) {
      void *result = nil;
      object_getInstanceVariable([UIApplication sharedApplication], "_statusBar", &result);
      id status = result;
      for (id aview in [status subviews]) {
      for (id bview in [aview subviews]) {
      int batteryLevel = 0;
      if ([NSStringFromClass([bview class]) caseInsensitiveCompare:@"UIStatusBarBatteryItemView"] == NSOrderedSame&&[[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0)
      {
      object_getInstanceVariable(bview, "_capacity", &result);
      batteryLevel = (int)result;
      NSLog(@"电池电量:%d",batteryLevel);
      if (batteryLevel > 0 && batteryLevel <= 100) {
      return batteryLevel;

              } else {  
                  return 0;  
              }  
          }  

      }
      }

    return 0;
    }

2016-01-12 16:11 负责人:无 分享
已邀请:
球风无痕

球风无痕 (作者)

没人帮忙吗?

DCloud_IOS_XTY

DCloud_IOS_XTY

var UIDevice = plus.ios.import("UIDevice");  
var dev = UIDevice.currentDevice();  
if (!dev.isBatteryMonitoringEnabled()) {  
    dev.setBatteryMonitoringEnabled(true);  
}  
var level =dev.batteryLevel();

该问题目前已经被锁定, 无法添加新回复