Update Android part of flutter_view to use the new platform message c… (#8729)
* update Android part of flutter_view to use the new platform message channel. * addressed comments * addressed comments
This commit is contained in:
@@ -6,6 +6,10 @@ import android.support.design.widget.FloatingActionButton;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.view.View;
|
||||
import android.widget.TextView;
|
||||
import io.flutter.plugin.common.FlutterMessageChannel;
|
||||
import io.flutter.plugin.common.FlutterMessageChannel.MessageHandler;
|
||||
import io.flutter.plugin.common.FlutterMessageChannel.Reply;
|
||||
import io.flutter.plugin.common.StringMessageCodec;
|
||||
import io.flutter.view.FlutterMain;
|
||||
import io.flutter.view.FlutterView;
|
||||
import java.util.ArrayList;
|
||||
@@ -15,6 +19,8 @@ public class MainActivity extends AppCompatActivity {
|
||||
private int counter;
|
||||
private static final String CHANNEL = "increment";
|
||||
private static final String EMPTY_MESSAGE = "";
|
||||
private static final String PING = "ping";
|
||||
private FlutterMessageChannel messageChannel;
|
||||
|
||||
private String[] getArgsFromIntent(Intent intent) {
|
||||
// Before adding more entries to this list, consider that arbitrary
|
||||
@@ -51,11 +57,15 @@ public class MainActivity extends AppCompatActivity {
|
||||
flutterView = (FlutterView) findViewById(R.id.flutter_view);
|
||||
flutterView.runFromBundle(FlutterMain.findAppBundlePath(getApplicationContext()), null);
|
||||
|
||||
flutterView.addOnMessageListener(CHANNEL,
|
||||
new FlutterView.OnMessageListener() {
|
||||
messageChannel =
|
||||
new FlutterMessageChannel<String>(flutterView, CHANNEL, StringMessageCodec.INSTANCE);
|
||||
messageChannel.
|
||||
setMessageHandler(new MessageHandler<String>() {
|
||||
@Override
|
||||
public String onMessage(FlutterView view, String message) {
|
||||
return onFlutterIncrement();
|
||||
public void onMessage(String s, Reply<String> reply) {
|
||||
onFlutterIncrement();
|
||||
reply.send(EMPTY_MESSAGE);
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
@@ -69,15 +79,14 @@ public class MainActivity extends AppCompatActivity {
|
||||
}
|
||||
|
||||
private void sendAndroidIncrement() {
|
||||
flutterView.sendToFlutter(CHANNEL, EMPTY_MESSAGE, null);
|
||||
messageChannel.send(PING);
|
||||
}
|
||||
|
||||
private String onFlutterIncrement() {
|
||||
private void onFlutterIncrement() {
|
||||
counter++;
|
||||
TextView textView = (TextView) findViewById(R.id.button_tap);
|
||||
String value = "Flutter button tapped " + counter + (counter == 1 ? " time" : " times");
|
||||
textView.setText(value);
|
||||
return EMPTY_MESSAGE;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user