zoukankan      html  css  js  c++  java
  • flutter 调用原生(获取当前设备电池电量)

    代码:

    import 'package:flutter/material.dart';
    import 'package:flutter/services.dart';
    import 'dart:async';
    
    
    class BatteryPage extends StatefulWidget {
      @override
      _BatteryPageState createState() {
        return new _BatteryPageState();
      }
    }
    
    class _BatteryPageState extends State<BatteryPage> {
      static const platform = const MethodChannel('samples.flutter.io/battery');
    
      String _batteryLevel = 'Unknown battery level.';
    
      Future<Null> _getBatteryLevel() async {
        String batteryLevel;
        try {
          final int result = await platform.invokeMethod('getBatteryLevel');
          batteryLevel = 'Battery level at $result % .';
        } on PlatformException catch (e) {
          batteryLevel = "Failed to get battery level: '${e.message}'.";
        }
    
        setState(() {
          _batteryLevel = batteryLevel;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return new Material(
          child: new Center(
            child: new Column(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                new RaisedButton(
                  onPressed: _getBatteryLevel,
                  child: new Text('Get Battery Level'),
                ),
                new Text(_batteryLevel),
              ],
            ),
          ),
        );
      }
    }

    iOS 的代码

    AppDelegate.m

    #include "AppDelegate.h"
    #include "GeneratedPluginRegistrant.h"
    
    @implementation AppDelegate
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
      [GeneratedPluginRegistrant registerWithRegistry:self];
      // Override point for customization after application launch.
        
        FlutterViewController* controller = (FlutterViewController*)self.window.rootViewController;
        FlutterMethodChannel* batteryChannel = [FlutterMethodChannel methodChannelWithName:@"samples.flutter.io/battery" binaryMessenger:controller];
        
        [batteryChannel setMethodCallHandler:^(FlutterMethodCall * _Nonnull call, FlutterResult  _Nonnull result) {
            if ([@"getBatteryLevel" isEqualToString:call.method]) {
                int batteryLevel = [self getBatteryLabel];
                if (batteryLevel == -1) {
                    result( [FlutterError errorWithCode:@"UNAVAILABLE" message:@"Battery info unavailable" details:nil]);
                } else {
                    result (@(batteryLevel));
                }
            } else {
                result(FlutterMethodNotImplemented);
            }
        }];
        
        return [super application:application didFinishLaunchingWithOptions:launchOptions];
    }
    
    - (int) getBatteryLabel {
        UIDevice* device = [UIDevice currentDevice];
        device.batteryMonitoringEnabled = YES;
        if (device.batteryState == UIDeviceBatteryStateUnknown) {
            return -1;
        } else {
            return (int)(device.batteryLevel * 100);
        }
    }
    
    @end

    Android的代码

    (以后再补充。。。)

  • 相关阅读:
    php学习笔记
    附加题-重构的读后总结
    附加题-stack的理解
    (转)php的扩展和嵌入--php的生命周期与变量详述
    homework-09
    html学习笔记之position
    homework-06
    homework-08
    在windows下使用git需要反复输入用户名和密码的问题
    windows命令行编码与nodejs编码格式冲突的解决方式
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/10244468.html
Copyright © 2011-2022 走看看