mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00
Update platform_services to query battery level instead of location. (#8554)
This commit is contained in:
parent
bbf84b1d69
commit
63b9f56c55
@ -6,61 +6,12 @@ You can read more about
|
|||||||
[accessing platform and third-party services in Flutter](https://flutter.io/platform-services/).
|
[accessing platform and third-party services in Flutter](https://flutter.io/platform-services/).
|
||||||
|
|
||||||
## iOS
|
## iOS
|
||||||
|
You can use the commands `flutter build` and `flutter run` from the app's root
|
||||||
### Configure
|
directory to build/run the app or you can open `ios/Runner.xcworkspace` in Xcode
|
||||||
|
and build/run the project as usual.
|
||||||
Create an `ios/Flutter/Generated.xcconfig` file with this entry:
|
|
||||||
|
|
||||||
* `FLUTTER_ROOT=[absolute path to the Flutter SDK]`
|
|
||||||
|
|
||||||
There are a number of other parameters you can control with this file:
|
|
||||||
|
|
||||||
* `FLUTTER_APPLICATION_PATH`: The path to the directory that contains your
|
|
||||||
`pubspec.yaml` file relative to your `xcodeproj` file.
|
|
||||||
* `FLUTTER_BUILD_MODE`: Whether to build for `debug`, `profile`, or `release`.
|
|
||||||
Defaults to `release`.
|
|
||||||
* `FLUTTER_TARGET`: The path to your `main.dart` relative to your
|
|
||||||
`pubspec.yaml`. Defaults to `lib/main.dart`.
|
|
||||||
* `FLUTTER_FRAMEWORK_DIR`: The absolute path to the directory that contains
|
|
||||||
`Flutter.framework`. Defaults to the `ios-release` version of
|
|
||||||
`Flutter.framework` in the `bin/cache` directory of the Flutter SDK.
|
|
||||||
|
|
||||||
### Build
|
|
||||||
|
|
||||||
Once you've configured your project, you can open `ios/Runner.xcodeproj`
|
|
||||||
in Xcode and build the project as usual.
|
|
||||||
|
|
||||||
## Android
|
## Android
|
||||||
|
|
||||||
### Configure
|
You can use the commands `flutter build` and `flutter run` from the app's root
|
||||||
|
directory to build/run the app or to build with Android Studio, open the
|
||||||
Create an `android/local.properties` file with these entries:
|
`android` folder in Android Studio and build the project as usual.
|
||||||
|
|
||||||
* `sdk.dir=[path to the Android SDK]`
|
|
||||||
* `flutter.sdk=[path to the Flutter SDK]`
|
|
||||||
|
|
||||||
There are a number of other parameters you can control with this file:
|
|
||||||
|
|
||||||
* `flutter.buildMode`: Whether to build for `debug`, `profile`, or `release`.
|
|
||||||
Defaults to `release`.
|
|
||||||
* `flutter.jar`: The path to `flutter.jar`. Defaults to the
|
|
||||||
`android-arm-release` version of `flutter.jar` in the `bin/cache` directory
|
|
||||||
of the Flutter SDK.
|
|
||||||
|
|
||||||
See `android/app/build.gradle` for project specific settings, including:
|
|
||||||
|
|
||||||
* `source`: The path to the directory that contains your `pubspec.yaml` file
|
|
||||||
relative to your `build.gradle` file.
|
|
||||||
* `target`: The path to your `main.dart` relative to your `pubspec.yaml`.
|
|
||||||
Defaults to `lib/main.dart`.
|
|
||||||
|
|
||||||
### Build
|
|
||||||
|
|
||||||
To build directly with `gradle`, use the following commands:
|
|
||||||
|
|
||||||
* `cd android`
|
|
||||||
* `gradle wrapper`
|
|
||||||
* `./gradlew build`
|
|
||||||
|
|
||||||
To build with Android Studio, open the `android` folder in Android Studio and
|
|
||||||
build the project as usual.
|
|
||||||
|
@ -4,10 +4,9 @@
|
|||||||
|
|
||||||
package com.example.flutter;
|
package com.example.flutter;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.os.BatteryManager;
|
||||||
import android.content.pm.PackageManager;
|
import android.os.Build.VERSION;
|
||||||
import android.location.Location;
|
import android.os.Build.VERSION_CODES;
|
||||||
import android.location.LocationManager;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
|
||||||
import io.flutter.app.FlutterActivity;
|
import io.flutter.app.FlutterActivity;
|
||||||
@ -15,49 +14,33 @@ import io.flutter.plugin.common.FlutterMethodChannel;
|
|||||||
import io.flutter.plugin.common.FlutterMethodChannel.MethodCallHandler;
|
import io.flutter.plugin.common.FlutterMethodChannel.MethodCallHandler;
|
||||||
import io.flutter.plugin.common.FlutterMethodChannel.Response;
|
import io.flutter.plugin.common.FlutterMethodChannel.Response;
|
||||||
import io.flutter.plugin.common.MethodCall;
|
import io.flutter.plugin.common.MethodCall;
|
||||||
import io.flutter.view.FlutterView;
|
|
||||||
|
|
||||||
public class ExampleActivity extends FlutterActivity {
|
public class ExampleActivity extends FlutterActivity {
|
||||||
private FlutterView flutterView;
|
private static final String CHANNEL = "battery";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
new FlutterMethodChannel(getFlutterView(), "geo").setMethodCallHandler(new MethodCallHandler() {
|
|
||||||
@Override
|
|
||||||
public void onMethodCall(MethodCall call, Response response) {
|
|
||||||
if (call.method.equals("getLocation")) {
|
|
||||||
if (!(call.arguments instanceof String)) {
|
|
||||||
throw new IllegalArgumentException("Invalid argument type, String expected");
|
|
||||||
}
|
|
||||||
getLocation((String) call.arguments, response);
|
|
||||||
} else {
|
|
||||||
throw new IllegalArgumentException("Unknown method " + call.method);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private void getLocation(String provider, Response response) {
|
new FlutterMethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
|
||||||
String locationProvider;
|
new MethodCallHandler() {
|
||||||
if (provider.equals("network")) {
|
@Override
|
||||||
locationProvider = LocationManager.NETWORK_PROVIDER;
|
public void onMethodCall(MethodCall call, Response response) {
|
||||||
} else if (provider.equals("gps")) {
|
if (call.method.equals("getBatteryLevel")) {
|
||||||
locationProvider = LocationManager.GPS_PROVIDER;
|
getBatteryLevel(response);
|
||||||
} else {
|
|
||||||
throw new IllegalArgumentException("Unknown provider " + provider);
|
|
||||||
}
|
|
||||||
String permission = "android.permission.ACCESS_FINE_LOCATION";
|
|
||||||
if (checkCallingOrSelfPermission(permission) == PackageManager.PERMISSION_GRANTED) {
|
|
||||||
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
|
|
||||||
Location location = locationManager.getLastKnownLocation(locationProvider);
|
|
||||||
if (location != null) {
|
|
||||||
response.success(new double[] { location.getLatitude(), location.getLongitude() });
|
|
||||||
} else {
|
} else {
|
||||||
response.error("unknown", "Location unknown", null);
|
throw new IllegalArgumentException("Unknown method " + call.method);
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
response.error("permission", "Access denied", null);
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void getBatteryLevel(Response response) {
|
||||||
|
BatteryManager batteryManager = (BatteryManager) getSystemService(BATTERY_SERVICE);
|
||||||
|
if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
|
||||||
|
response.success(batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY));
|
||||||
|
} else {
|
||||||
|
response.error("Not available", "Battery level not available.", null);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,6 @@
|
|||||||
9740EEB41CF90195004384FC /* Flutter.xcconfig in Resources */ = {isa = PBXBuildFile; fileRef = 9740EEB21CF90195004384FC /* Flutter.xcconfig */; };
|
9740EEB41CF90195004384FC /* Flutter.xcconfig in Resources */ = {isa = PBXBuildFile; fileRef = 9740EEB21CF90195004384FC /* Flutter.xcconfig */; };
|
||||||
9740EEB51CF90195004384FC /* Generated.xcconfig in Resources */ = {isa = PBXBuildFile; fileRef = 9740EEB31CF90195004384FC /* Generated.xcconfig */; };
|
9740EEB51CF90195004384FC /* Generated.xcconfig in Resources */ = {isa = PBXBuildFile; fileRef = 9740EEB31CF90195004384FC /* Generated.xcconfig */; };
|
||||||
9740EEBB1CF902C7004384FC /* app.flx in Resources */ = {isa = PBXBuildFile; fileRef = 9740EEB71CF902C7004384FC /* app.flx */; };
|
9740EEBB1CF902C7004384FC /* app.flx in Resources */ = {isa = PBXBuildFile; fileRef = 9740EEB71CF902C7004384FC /* app.flx */; };
|
||||||
977505191CFDF23500BC28DA /* LocationProvider.m in Sources */ = {isa = PBXBuildFile; fileRef = 977505181CFDF23500BC28DA /* LocationProvider.m */; };
|
|
||||||
97A38A351CFDEC880099F1B4 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 97A38A341CFDEC880099F1B4 /* AppDelegate.m */; };
|
97A38A351CFDEC880099F1B4 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 97A38A341CFDEC880099F1B4 /* AppDelegate.m */; };
|
||||||
97C146F31CF9000F007C117D /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 97C146F21CF9000F007C117D /* main.m */; };
|
97C146F31CF9000F007C117D /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 97C146F21CF9000F007C117D /* main.m */; };
|
||||||
97C146FC1CF9000F007C117D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FA1CF9000F007C117D /* Main.storyboard */; };
|
97C146FC1CF9000F007C117D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FA1CF9000F007C117D /* Main.storyboard */; };
|
||||||
@ -42,8 +41,6 @@
|
|||||||
9740EEB71CF902C7004384FC /* app.flx */ = {isa = PBXFileReference; lastKnownFileType = file; name = app.flx; path = Flutter/app.flx; sourceTree = "<group>"; };
|
9740EEB71CF902C7004384FC /* app.flx */ = {isa = PBXFileReference; lastKnownFileType = file; name = app.flx; path = Flutter/app.flx; sourceTree = "<group>"; };
|
||||||
9740EEB81CF902C7004384FC /* app.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = app.dylib; path = Flutter/app.dylib; sourceTree = "<group>"; };
|
9740EEB81CF902C7004384FC /* app.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = app.dylib; path = Flutter/app.dylib; sourceTree = "<group>"; };
|
||||||
9740EEBA1CF902C7004384FC /* Flutter.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Flutter.framework; path = Flutter/Flutter.framework; sourceTree = "<group>"; };
|
9740EEBA1CF902C7004384FC /* Flutter.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Flutter.framework; path = Flutter/Flutter.framework; sourceTree = "<group>"; };
|
||||||
977505171CFDF21E00BC28DA /* LocationProvider.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LocationProvider.h; sourceTree = "<group>"; };
|
|
||||||
977505181CFDF23500BC28DA /* LocationProvider.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LocationProvider.m; sourceTree = "<group>"; };
|
|
||||||
97A38A331CFDEC680099F1B4 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
|
97A38A331CFDEC680099F1B4 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
|
||||||
97A38A341CFDEC880099F1B4 /* AppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
|
97A38A341CFDEC880099F1B4 /* AppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
|
||||||
97C146EE1CF9000F007C117D /* Runner.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Runner.app; sourceTree = BUILT_PRODUCTS_DIR; };
|
97C146EE1CF9000F007C117D /* Runner.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Runner.app; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||||
@ -105,8 +102,6 @@
|
|||||||
97C146F11CF9000F007C117D /* Supporting Files */,
|
97C146F11CF9000F007C117D /* Supporting Files */,
|
||||||
97A38A331CFDEC680099F1B4 /* AppDelegate.h */,
|
97A38A331CFDEC680099F1B4 /* AppDelegate.h */,
|
||||||
97A38A341CFDEC880099F1B4 /* AppDelegate.m */,
|
97A38A341CFDEC880099F1B4 /* AppDelegate.m */,
|
||||||
977505171CFDF21E00BC28DA /* LocationProvider.h */,
|
|
||||||
977505181CFDF23500BC28DA /* LocationProvider.m */,
|
|
||||||
);
|
);
|
||||||
path = Runner;
|
path = Runner;
|
||||||
sourceTree = "<group>";
|
sourceTree = "<group>";
|
||||||
@ -212,7 +207,6 @@
|
|||||||
files = (
|
files = (
|
||||||
97A38A351CFDEC880099F1B4 /* AppDelegate.m in Sources */,
|
97A38A351CFDEC880099F1B4 /* AppDelegate.m in Sources */,
|
||||||
97C146F31CF9000F007C117D /* main.m in Sources */,
|
97C146F31CF9000F007C117D /* main.m in Sources */,
|
||||||
977505191CFDF23500BC28DA /* LocationProvider.m in Sources */,
|
|
||||||
);
|
);
|
||||||
runOnlyForDeploymentPostprocessing = 0;
|
runOnlyForDeploymentPostprocessing = 0;
|
||||||
};
|
};
|
||||||
|
@ -5,23 +5,11 @@
|
|||||||
#import "AppDelegate.h"
|
#import "AppDelegate.h"
|
||||||
|
|
||||||
#import <Flutter/Flutter.h>
|
#import <Flutter/Flutter.h>
|
||||||
#import "LocationProvider.h"
|
|
||||||
|
|
||||||
@implementation AppDelegate {
|
@implementation AppDelegate {
|
||||||
LocationProvider* _locationProvider;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
|
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
|
||||||
FlutterDartProject* project = [[FlutterDartProject alloc] initFromDefaultSourceForConfiguration];
|
|
||||||
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
|
|
||||||
FlutterViewController* flutterController = [[FlutterViewController alloc] initWithProject:project
|
|
||||||
nibName:nil
|
|
||||||
bundle:nil];
|
|
||||||
_locationProvider = [[LocationProvider alloc] init];
|
|
||||||
[flutterController addMessageListener:_locationProvider];
|
|
||||||
|
|
||||||
self.window.rootViewController = flutterController;
|
|
||||||
[self.window makeKeyAndVisible];
|
|
||||||
return YES;
|
return YES;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,21 +1,26 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6211" systemVersion="14A298i" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
|
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11762" systemVersion="16D32" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
|
||||||
|
<device id="retina4_7" orientation="portrait">
|
||||||
|
<adaptation id="fullscreen"/>
|
||||||
|
</device>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6204"/>
|
<deployment identifier="iOS"/>
|
||||||
|
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11757"/>
|
||||||
|
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<scenes>
|
<scenes>
|
||||||
<!--View Controller-->
|
<!--Flutter View Controller-->
|
||||||
<scene sceneID="tne-QT-ifu">
|
<scene sceneID="tne-QT-ifu">
|
||||||
<objects>
|
<objects>
|
||||||
<viewController id="BYZ-38-t0r" customClass="ViewController" customModuleProvider="" sceneMemberID="viewController">
|
<viewController id="BYZ-38-t0r" customClass="FlutterViewController" sceneMemberID="viewController">
|
||||||
<layoutGuides>
|
<layoutGuides>
|
||||||
<viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
|
<viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
|
||||||
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
|
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
|
||||||
</layoutGuides>
|
</layoutGuides>
|
||||||
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
|
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
|
||||||
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
|
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
|
||||||
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
|
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
|
||||||
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
|
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
|
||||||
</view>
|
</view>
|
||||||
</viewController>
|
</viewController>
|
||||||
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
|
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
|
||||||
|
@ -1,9 +0,0 @@
|
|||||||
// Copyright 2016 The Chromium Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style license that can be
|
|
||||||
// found in the LICENSE file.
|
|
||||||
|
|
||||||
#import <Flutter/Flutter.h>
|
|
||||||
|
|
||||||
@interface LocationProvider : NSObject <FlutterMessageListener>
|
|
||||||
|
|
||||||
@end
|
|
@ -1,39 +0,0 @@
|
|||||||
// Copyright 2016 The Chromium Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style license that can be
|
|
||||||
// found in the LICENSE file.
|
|
||||||
|
|
||||||
#import "LocationProvider.h"
|
|
||||||
|
|
||||||
#import <CoreLocation/CoreLocation.h>
|
|
||||||
|
|
||||||
@implementation LocationProvider {
|
|
||||||
CLLocationManager* _locationManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
@synthesize messageName = _messageName;
|
|
||||||
|
|
||||||
- (instancetype) init {
|
|
||||||
self = [super init];
|
|
||||||
if (self)
|
|
||||||
self->_messageName = @"getLocation";
|
|
||||||
return self;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (NSString*)didReceiveString:(NSString*)message {
|
|
||||||
if (_locationManager == nil) {
|
|
||||||
_locationManager = [[CLLocationManager alloc] init];
|
|
||||||
[_locationManager startMonitoringSignificantLocationChanges];
|
|
||||||
}
|
|
||||||
|
|
||||||
CLLocation* location = _locationManager.location;
|
|
||||||
|
|
||||||
NSDictionary* response = @{
|
|
||||||
@"latitude": @(location.coordinate.latitude),
|
|
||||||
@"longitude": @(location.coordinate.longitude),
|
|
||||||
};
|
|
||||||
|
|
||||||
NSData* data = [NSJSONSerialization dataWithJSONObject:response options:0 error:nil];
|
|
||||||
return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
|
|
||||||
}
|
|
||||||
|
|
||||||
@end
|
|
@ -3,6 +3,7 @@
|
|||||||
// found in the LICENSE file.
|
// found in the LICENSE file.
|
||||||
|
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
import 'dart:io';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
@ -13,20 +14,23 @@ class PlatformServices extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _PlatformServicesState extends State<PlatformServices> {
|
class _PlatformServicesState extends State<PlatformServices> {
|
||||||
static const PlatformMethodChannel platform = const PlatformMethodChannel('geo');
|
static const PlatformMethodChannel platform = const PlatformMethodChannel('battery');
|
||||||
String _location = 'Unknown location.';
|
String _batteryLevel = 'Unknown battery level.';
|
||||||
|
|
||||||
Future<Null> _getLocation() async {
|
Future<Null> _getBatteryLevel() async {
|
||||||
String location;
|
String batteryLevel;
|
||||||
try {
|
if (Platform.isIOS) {
|
||||||
final List<double> result = await platform.invokeMethod('getLocation', 'network');
|
batteryLevel = "iOS is not supported yet.";
|
||||||
location = 'Latitude ${result[0]}, Longitude ${result[1]}.';
|
} else {
|
||||||
} on PlatformException catch (e) {
|
try {
|
||||||
location = "Failed to get location: '${e.message}'.";
|
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(() {
|
setState(() {
|
||||||
_location = location;
|
_batteryLevel = batteryLevel;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,17 +42,17 @@ class _PlatformServicesState extends State<PlatformServices> {
|
|||||||
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
||||||
children: <Widget>[
|
children: <Widget>[
|
||||||
new RaisedButton(
|
new RaisedButton(
|
||||||
child: new Text('Get Location'),
|
child: new Text('Get Battery Level'),
|
||||||
onPressed: _getLocation,
|
onPressed: _getBatteryLevel,
|
||||||
),
|
),
|
||||||
new Text(_location)
|
new Text(_batteryLevel)
|
||||||
],
|
],
|
||||||
),
|
)
|
||||||
),
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
runApp(new PlatformServices());
|
runApp(new PlatformServices());
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user