Merge pull request #2869 from jason-simmons/gradle_send_receive
An example showing how to send messages between the host app and Flutter
This commit is contained in:
@@ -2,6 +2,71 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:math';
|
||||
|
||||
void main() => runApp(new Center(child: new Text('Hello from Flutter!')));
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
final Random random = new Random();
|
||||
|
||||
Future<String> handleGetRandom(String json) async {
|
||||
Map message = JSON.decode(json);
|
||||
double min = message['min'].toDouble();
|
||||
double max = message['max'].toDouble();
|
||||
|
||||
double value = (random.nextDouble() * (max - min)) + min;
|
||||
|
||||
Map reply = {'value': value};
|
||||
return JSON.encode(reply);
|
||||
}
|
||||
|
||||
class HelloAndroid extends StatefulWidget {
|
||||
@override
|
||||
_HelloAndroidState createState() => new _HelloAndroidState();
|
||||
}
|
||||
|
||||
class _HelloAndroidState extends State<HelloAndroid> {
|
||||
double _latitude;
|
||||
double _longitude;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return new Material(
|
||||
child: new Center(
|
||||
child: new Column(
|
||||
children: <Widget>[
|
||||
new Text('Hello from Flutter!'),
|
||||
new RaisedButton(
|
||||
child: new Text('Get Location'),
|
||||
onPressed: _getLocation
|
||||
),
|
||||
new Text('Latitude: ${_latitude}, Longitude: ${_longitude}'),
|
||||
]
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
void _getLocation() {
|
||||
Map message = {'provider': 'network'};
|
||||
HostMessages.sendToHost('getLocation', JSON.encode(message))
|
||||
.then(_onReceivedLocation);
|
||||
}
|
||||
|
||||
void _onReceivedLocation(String json) {
|
||||
Map reply = JSON.decode(json);
|
||||
setState(() {
|
||||
_latitude = reply['latitude'];
|
||||
_longitude = reply['longitude'];
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
runApp(new HelloAndroid());
|
||||
|
||||
HostMessages.addMessageHandler('getRandom', handleGetRandom);
|
||||
}
|
||||
|
||||
@@ -6,14 +6,15 @@
|
||||
|
||||
<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="21" />
|
||||
<uses-permission android:name="android.permission.INTERNET"/>
|
||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
|
||||
|
||||
<application android:name="org.domokit.sky.shell.SkyApplication" android:label="@string/app_name" >
|
||||
<activity
|
||||
android:name=".FlutterActivity"
|
||||
android:label="@string/app_name"
|
||||
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection"
|
||||
android:name=".ExampleActivity"
|
||||
android:launchMode="singleTask"
|
||||
android:theme="@android:style/Theme.Black.NoTitleBar"
|
||||
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection"
|
||||
android:hardwareAccelerated="true"
|
||||
android:windowSoftInputMode="adjustResize">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
// 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.
|
||||
|
||||
package com.example.flutter;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.location.Location;
|
||||
import android.location.LocationManager;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.chromium.base.PathUtils;
|
||||
import org.domokit.activity.ActivityImpl;
|
||||
import org.domokit.sky.shell.SkyMain;
|
||||
import org.domokit.sky.shell.PlatformViewAndroid;
|
||||
|
||||
import java.io.File;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class ExampleActivity extends Activity {
|
||||
private static final String TAG = "ExampleActivity";
|
||||
|
||||
private PlatformViewAndroid flutterView;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
SkyMain.ensureInitialized(getApplicationContext(), null);
|
||||
setContentView(R.layout.flutter_layout);
|
||||
|
||||
flutterView = (PlatformViewAndroid) findViewById(R.id.flutter_view);
|
||||
File appBundle = new File(PathUtils.getDataDirectory(this), SkyMain.APP_BUNDLE);
|
||||
flutterView.runFromBundle(appBundle.getPath(), null);
|
||||
|
||||
flutterView.addOnMessageListener("getLocation",
|
||||
new PlatformViewAndroid.OnMessageListener() {
|
||||
@Override
|
||||
public String onMessage(String message) {
|
||||
return onGetLocation(message);
|
||||
}
|
||||
});
|
||||
|
||||
Button getRandom = (Button) findViewById(R.id.get_random);
|
||||
getRandom.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
sendGetRandom();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
if (flutterView != null) {
|
||||
flutterView.destroy();
|
||||
}
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
flutterView.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
flutterView.onResume();
|
||||
}
|
||||
|
||||
private void sendGetRandom() {
|
||||
JSONObject message = new JSONObject();
|
||||
try {
|
||||
message.put("min", 1);
|
||||
message.put("max", 1000);
|
||||
} catch (JSONException e) {
|
||||
Log.e(TAG, "JSON exception", e);
|
||||
return;
|
||||
}
|
||||
|
||||
flutterView.sendToFlutter("getRandom", message.toString(),
|
||||
new PlatformViewAndroid.MessageReplyCallback() {
|
||||
@Override
|
||||
public void onReply(String json) {
|
||||
onRandomReply(json);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void onRandomReply(String json) {
|
||||
double value;
|
||||
try {
|
||||
JSONObject reply = new JSONObject(json);
|
||||
value = reply.getDouble("value");
|
||||
} catch (JSONException e) {
|
||||
Log.e(TAG, "JSON exception", e);
|
||||
return;
|
||||
}
|
||||
|
||||
TextView randomValue = (TextView) findViewById(R.id.random_value);
|
||||
randomValue.setText(Double.toString(value));
|
||||
}
|
||||
|
||||
private String onGetLocation(String json) {
|
||||
String provider;
|
||||
try {
|
||||
JSONObject message = new JSONObject(json);
|
||||
provider = message.getString("provider");
|
||||
} catch (JSONException e) {
|
||||
Log.e(TAG, "JSON exception", e);
|
||||
return null;
|
||||
}
|
||||
|
||||
String locationProvider;
|
||||
if (provider.equals("network")) {
|
||||
locationProvider = LocationManager.NETWORK_PROVIDER;
|
||||
} else if (provider.equals("gps")) {
|
||||
locationProvider = LocationManager.GPS_PROVIDER;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
LocationManager locationManager =
|
||||
(LocationManager) getSystemService(Context.LOCATION_SERVICE);
|
||||
Location location = locationManager.getLastKnownLocation(locationProvider);
|
||||
|
||||
JSONObject reply = new JSONObject();
|
||||
try {
|
||||
reply.put("latitude", location.getLatitude());
|
||||
reply.put("longitude", location.getLongitude());
|
||||
} catch (JSONException e) {
|
||||
Log.e(TAG, "JSON exception", e);
|
||||
return null;
|
||||
}
|
||||
|
||||
return reply.toString();
|
||||
}
|
||||
}
|
||||
@@ -1,38 +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.
|
||||
|
||||
package com.example.flutter;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
|
||||
import org.chromium.base.PathUtils;
|
||||
import org.domokit.sky.shell.SkyMain;
|
||||
import org.domokit.sky.shell.PlatformViewAndroid;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class FlutterActivity extends Activity {
|
||||
private PlatformViewAndroid flutterView;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
SkyMain.ensureInitialized(getApplicationContext(), null);
|
||||
setContentView(R.layout.flutter_layout);
|
||||
|
||||
flutterView = (PlatformViewAndroid) findViewById(R.id.flutter_view);
|
||||
File appBundle = new File(PathUtils.getDataDirectory(this), SkyMain.APP_BUNDLE);
|
||||
flutterView.runFromBundle(appBundle.getPath(), null);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
if (flutterView != null) {
|
||||
flutterView.destroy();
|
||||
}
|
||||
super.onDestroy();
|
||||
}
|
||||
}
|
||||
@@ -4,15 +4,36 @@
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="fill_parent"
|
||||
>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/text_view"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/title"
|
||||
/>
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="horizontal"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
>
|
||||
<Button
|
||||
android:id="@+id/get_random"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/get_random"
|
||||
/>
|
||||
<TextView
|
||||
android:id="@+id/random_value"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
/>
|
||||
</LinearLayout>
|
||||
|
||||
<org.domokit.sky.shell.PlatformViewAndroid
|
||||
android:id="@+id/flutter_view"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="fill_parent"
|
||||
/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
@@ -2,4 +2,5 @@
|
||||
<resources>
|
||||
<string name="app_name">Flutter App</string>
|
||||
<string name="title">Flutter Application</string>
|
||||
<string name="get_random">Get Random Number</string>
|
||||
</resources>
|
||||
|
||||
Reference in New Issue
Block a user