iOS/Andorid电量监听

iOS

#pragma mark - 电池电量获取及监控
-(void)checkAndMonitorBatteryLevel{
    //拿到当前设备
    UIDevice * device = [UIDevice currentDevice];
    //是否允许监测电池
    //要想获取电池电量信息和监控电池电量 必须允许
    device.batteryMonitoringEnabled = true;
    //1、check
    /*
     获取电池电量
     0 .. 1.0. -1.0 if UIDeviceBatteryStateUnknown
     */
    float level = device.batteryLevel;
    NSLog(@"level = %lf",level);
    //2、monitor
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didChangeBatteryLevel:) name:@"UIDeviceBatteryLevelDidChangeNotification" object:device];
}

- (void)didChangeBatteryLevel:(id)sender{
    //电池电量发生改变时调用
    UIDevice *myDevice = [UIDevice currentDevice];
    [myDevice setBatteryMonitoringEnabled:YES];
    float batteryLevel = [myDevice batteryLevel];
    NSLog(@"电池剩余比例:%@", [NSString stringWithFormat:@"%f",batteryLevel*100]);
}
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didChangeBatteryLevel:) name:@"UIDeviceBatteryLevelDidChangeNotification" object:device];

Android:

    private BroadcastReceiver myBatteryReceiver =
        new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                int bLevel = arg1.getIntExtra("level", 0);// the battery level in integer
                //添加自定义回调
            }
        };

你可能感兴趣的:(iOS/Andorid电量监听)