flutter 和android 通信问题

package com.dy.flutter_yxy;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.ActionMode;
import android.view.Window;
import android.webkit.GeolocationPermissions;


import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.Objects;

import io.flutter.embedding.android.FlutterActivity;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterActivity {

    static String CHANNEL = "daishuming";

    private String getAndroidInfo() {
        return "我是奸细";
    }

    MethodChannel _methodChannel;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GeneratedPluginRegistrant.registerWith(Objects.requireNonNull(getFlutterEngine()));

        BinaryMessenger _messenger = getFlutterEngine().getDartExecutor().getBinaryMessenger();
        _methodChannel = new MethodChannel(_messenger, CHANNEL);


        _methodChannel.setMethodCallHandler(new MethodChannel.MethodCallHandler() {
            @Override
            public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
                if ("getAndroidInfo".equals(call.method)) {
                    onIdentityAuthentication(new Callback() {
                        @Override
                        public void onSuccess() {
                            String _androidInfo = getAndroidInfo();
                            result.success(_androidInfo);
                        }

                        @Override
                        public void onError() {
                            result.success("身份认证出错,不认识你");
                        }
                    });
                }
            }
        });
    }


    private void onIdentityAuthentication(Callback callback) {
        _methodChannel.invokeMethod("IdentityAuthentication", null, new MethodChannel.Result() {
            @Override
            public void success(@Nullable Object result) {
                if (result instanceof String) {
                    if ("daiyao".equals(result)) {
                        callback.onSuccess();
                    }else{
                        callback.onError();
                    }
                }
            }

            @Override
            public void error(String errorCode, @Nullable String errorMessage, @Nullable Object errorDetails) {

            }

            @Override
            public void notImplemented() {

            }
        });
    }
}

interface Callback {
    void onSuccess();

    void onError();
}

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      top: true,
      child: Scaffold(
        body: FlatButton(
          child: Text("点击获取android信息"),
          onPressed: () async {
            MethodChannel _channel = MethodChannel("daishuming");
            _channel.setMethodCallHandler((MethodCall call) async {
              if (call.method == "IdentityAuthentication") {
                return "daiyao1";
              }
            });
            String _response = await _channel.invokeMethod("getAndroidInfo");


            print('接受到android探子回传的消息: $_response');
          },
        ),
      ),
    );
  }

你可能感兴趣的:(flutter 和android 通信问题)