Skip to content

Commit 2f168bd

Browse files
1. 将3个demo放在一个main()入口里,以支持命令行编译 2. 增加Android平台和Flutter通过消息系统传递数据的例子
1 parent 0494a2e commit 2f168bd

File tree

10 files changed

+584
-481
lines changed

10 files changed

+584
-481
lines changed

.packages

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,64 @@
1-
# Generated by pub on 2018-02-26 14:42:35.603887.
1+
# Generated by pub on 2018-03-02 15:57:13.495265.
22
analyzer:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/analyzer-0.30.0+4/lib/
3-
args:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/args-0.13.7/lib/
4-
async:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/async-1.13.3/lib/
5-
barback:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/barback-0.15.2+13/lib/
3+
args:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/args-1.3.0/lib/
4+
async:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/async-2.0.4/lib/
5+
barback:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/barback-0.15.2+14/lib/
66
boolean_selector:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/boolean_selector-1.0.2/lib/
77
charcode:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/charcode-1.1.1/lib/
88
cli_util:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/cli_util-0.1.2+1/lib/
9-
collection:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/collection-1.14.3/lib/
9+
collection:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/collection-1.14.5/lib/
1010
convert:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/convert-2.0.1/lib/
1111
crypto:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/crypto-2.0.2+1/lib/
1212
csslib:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/csslib-0.14.1/lib/
1313
cupertino_icons:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/cupertino_icons-0.1.1/lib/
14+
file:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/file-2.3.6/lib/
1415
flutter:file:///D:/flutter/packages/flutter/lib/
16+
flutter_driver:file:///D:/flutter/packages/flutter_driver/lib/
1517
flutter_test:file:///D:/flutter/packages/flutter_test/lib/
1618
front_end:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/front_end-0.1.0-alpha.4.1/lib/
1719
glob:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/glob-1.1.5/lib/
1820
html:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/html-0.13.2+2/lib/
19-
http:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/http-0.11.3+14/lib/
21+
http:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/http-0.11.3+16/lib/
2022
http_multi_server:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/http_multi_server-2.0.4/lib/
2123
http_parser:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/http_parser-3.1.1/lib/
22-
io:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/io-0.3.0/lib/
24+
intl:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/intl-0.15.2/lib/
25+
io:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/io-0.3.2+1/lib/
2326
isolate:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/isolate-1.1.0/lib/
2427
js:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/js-0.6.1/lib/
28+
json_rpc_2:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/json_rpc_2-2.0.7/lib/
2529
kernel:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/kernel-0.3.0-alpha.1.1/lib/
2630
logging:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/logging-0.11.3+1/lib/
2731
matcher:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/matcher-0.12.1+4/lib/
28-
meta:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/meta-1.1.1/lib/
29-
mime:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/mime-0.9.4/lib/
30-
mockito:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/mockito-2.2.0/lib/
32+
meta:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/meta-1.1.2/lib/
33+
mime:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/mime-0.9.6/lib/
34+
mockito:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/mockito-2.2.3/lib/
35+
multi_server_socket:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/multi_server_socket-1.0.1/lib/
3136
node_preamble:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/node_preamble-1.4.0/lib/
3237
package_config:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/package_config-1.0.3/lib/
3338
package_resolver:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/package_resolver-1.0.2/lib/
34-
path:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/path-1.4.2/lib/
39+
path:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/path-1.5.1/lib/
3540
plugin:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/plugin-0.2.0+2/lib/
36-
pool:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/pool-1.3.3/lib/
41+
pool:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/pool-1.3.4/lib/
3742
pub_semver:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/pub_semver-1.3.2/lib/
38-
quiver:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/quiver-0.26.2/lib/
39-
shelf:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/shelf-0.7.1/lib/
43+
quiver:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/quiver-0.28.0/lib/
44+
shelf:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/shelf-0.7.2/lib/
4045
shelf_packages_handler:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/shelf_packages_handler-1.0.3/lib/
41-
shelf_static:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/shelf_static-0.2.6/lib/
46+
shelf_static:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/shelf_static-0.2.7/lib/
4247
shelf_web_socket:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/shelf_web_socket-0.2.2/lib/
4348
sky_engine:file:///D:/flutter/bin/cache/pkg/sky_engine/lib/
4449
source_map_stack_trace:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/source_map_stack_trace-1.1.4/lib/
4550
source_maps:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/source_maps-0.10.4/lib/
4651
source_span:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/source_span-1.4.0/lib/
47-
stack_trace:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/stack_trace-1.9.0/lib/
48-
stream_channel:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/stream_channel-1.6.2/lib/
52+
stack_trace:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/stack_trace-1.9.1/lib/
53+
stream_channel:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/stream_channel-1.6.3/lib/
4954
string_scanner:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/string_scanner-1.0.2/lib/
5055
term_glyph:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/term_glyph-1.0.0/lib/
51-
test:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/test-0.12.26/lib/
52-
typed_data:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/typed_data-1.1.4/lib/
53-
utf:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/utf-0.9.0+3/lib/
56+
test:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/test-0.12.30+4/lib/
57+
typed_data:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/typed_data-1.1.5/lib/
58+
utf:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/utf-0.9.0+4/lib/
5459
vector_math:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/vector_math-2.0.5/lib/
55-
watcher:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/watcher-0.9.7+6/lib/
56-
web_socket_channel:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/web_socket_channel-1.0.6/lib/
60+
vm_service_client:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/vm_service_client-0.2.4+1/lib/
61+
watcher:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/watcher-0.9.7+7/lib/
62+
web_socket_channel:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/web_socket_channel-1.0.7/lib/
5763
yaml:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/yaml-2.1.13/lib/
5864
flutter_tv:lib/

android/app/src/main/java/com/tcl/flutter/tv/MainActivity.java

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,108 @@
11
package com.tcl.flutter.tv;
22

3+
import android.content.BroadcastReceiver;
4+
import android.content.Context;
5+
import android.content.ContextWrapper;
6+
import android.content.Intent;
7+
import android.content.IntentFilter;
8+
import android.os.BatteryManager;
9+
import android.os.Build.VERSION;
10+
import android.os.Build.VERSION_CODES;
311
import android.os.Bundle;
412
import android.util.Log;
513
import android.view.KeyEvent;
614

715
import io.flutter.app.FlutterActivity;
16+
import io.flutter.plugin.common.EventChannel;
17+
import io.flutter.plugin.common.EventChannel.EventSink;
18+
import io.flutter.plugin.common.EventChannel.StreamHandler;
19+
import io.flutter.plugin.common.MethodCall;
20+
import io.flutter.plugin.common.MethodChannel;
21+
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
22+
import io.flutter.plugin.common.MethodChannel.Result;
823
import io.flutter.plugins.GeneratedPluginRegistrant;
924

1025
public class MainActivity extends FlutterActivity {
1126

1227
private static final String TAG = MainActivity.class.getName();
28+
private static final String BATTERY_CHANNEL = "samples.flutter.io/battery";
29+
private static final String CHARGING_CHANNEL = "samples.flutter.io/charging";
30+
31+
private BroadcastReceiver chargingStateChangeReceiver;
1332

1433
@Override
1534
protected void onCreate(Bundle savedInstanceState) {
1635
super.onCreate(savedInstanceState);
1736
GeneratedPluginRegistrant.registerWith(this);
37+
new EventChannel(getFlutterView(), CHARGING_CHANNEL).setStreamHandler(
38+
new StreamHandler() {
39+
@Override
40+
public void onListen(Object arguments, EventSink events) {
41+
chargingStateChangeReceiver = createChargingStateChangeReceiver(events);
42+
registerReceiver(chargingStateChangeReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
43+
}
44+
45+
@Override
46+
public void onCancel(Object arguments) {
47+
unregisterReceiver(chargingStateChangeReceiver);
48+
chargingStateChangeReceiver = null;
49+
}
50+
}
51+
);
52+
53+
new MethodChannel(getFlutterView(), BATTERY_CHANNEL).setMethodCallHandler(
54+
new MethodCallHandler() {
55+
@Override
56+
public void onMethodCall(MethodCall call, Result result) {
57+
if (call.method.equals("getBatteryLevel")) {
58+
int batteryLevel = getBatteryLevel();
59+
60+
if (batteryLevel != -1) {
61+
result.success(batteryLevel);
62+
} else {
63+
result.error("UNAVAILABLE", "Battery level not available.", null);
64+
}
65+
} else {
66+
result.notImplemented();
67+
}
68+
}
69+
}
70+
);
71+
}
72+
73+
private BroadcastReceiver createChargingStateChangeReceiver(final EventSink events) {
74+
return new BroadcastReceiver() {
75+
@Override
76+
public void onReceive(Context context, Intent intent) {
77+
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
78+
79+
if (status == BatteryManager.BATTERY_STATUS_UNKNOWN) {
80+
events.error("UNAVAILABLE", "Charging status unavailable", null);
81+
} else {
82+
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
83+
status == BatteryManager.BATTERY_STATUS_FULL;
84+
events.success(isCharging ? "charging" : "discharging");
85+
}
86+
}
87+
};
88+
}
89+
90+
private int getBatteryLevel() {
91+
if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
92+
BatteryManager batteryManager = (BatteryManager) getSystemService(BATTERY_SERVICE);
93+
return batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
94+
} else {
95+
Intent intent = new ContextWrapper(getApplicationContext()).
96+
registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
97+
return (intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) * 100) /
98+
intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
99+
}
100+
}
101+
102+
@Override
103+
protected void onDestroy() {
104+
unregisterReceiver(chargingStateChangeReceiver);
105+
super.onDestroy();
18106
}
19107

20108
@Override

lib/http_request.dart

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,14 @@ import 'package:flutter/material.dart';
55
import 'package:flutter/services.dart';
66
import 'key_code.dart';
77

8-
void main() {
9-
runApp(new MyApp());
10-
}
11-
12-
class MyApp extends StatelessWidget {
13-
@override
14-
Widget build(BuildContext context) {
15-
return new MaterialApp(
16-
home: new MyHomePage(),
17-
);
18-
}
19-
}
20-
21-
class MyHomePage extends StatefulWidget {
22-
MyHomePage({Key key}) : super(key: key);
8+
class HttpDemo extends StatefulWidget {
9+
HttpDemo({Key key}) : super(key: key);
2310

2411
@override
25-
_MyHomePageState createState() => new _MyHomePageState();
12+
_HttpDemoState createState() => new _HttpDemoState();
2613
}
2714

28-
class _MyHomePageState extends State<MyHomePage> {
15+
class _HttpDemoState extends State<HttpDemo> {
2916
var _ipAddress = 'Unknown';
3017
FocusNode focusNode;
3118
bool _active = false;
@@ -81,6 +68,7 @@ class _MyHomePageState extends State<MyHomePage> {
8168
var spacer = new SizedBox(height: 32.0);
8269
FocusScope.of(context).requestFocus(focusNode);
8370
return new Scaffold(
71+
appBar: new AppBar(title: new Text('Http Demo')),
8472
body: new Center(
8573
child: new Column(
8674
mainAxisAlignment: MainAxisAlignment.center,

0 commit comments

Comments
 (0)