Skip to content

Instantly share code, notes, and snippets.

@ritshpatidar
Created June 28, 2019 17:34
Show Gist options
  • Save ritshpatidar/18094cd0e59b049f884a0c7efc75cf55 to your computer and use it in GitHub Desktop.
Save ritshpatidar/18094cd0e59b049f884a0c7efc75cf55 to your computer and use it in GitHub Desktop.
connecting java with flutter using method channel
package com.example.ourproject;
import android.os.Bundle;
import io.flutter.app.FlutterActivity;
import io.flutter.plugins.GeneratedPluginRegistrant;
import android.widget.Toast;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
public class MainActivity extends FlutterActivity {
private static final String CHANNEL = "ourproject.sendstring";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
new MethodCallHandler() {
@Override
public void onMethodCall(MethodCall call, Result result) {
if (call.method.equals("callSendStringFun")) {
showHelloFromFlutter(call.argument("arg"));
String temp = sendString();
result.success(temp);
} else {
result.notImplemented();
}
}
});
}
private String sendString(){
String stringToSend = "Hello from Kotlin";
return stringToSend;
}
private void showHelloFromFlutter(String argFromFlutter){
Toast.makeText(this, argFromFlutter, Toast.LENGTH_SHORT).show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment