美文网首页程序员
Flutter调用Android模块的功能

Flutter调用Android模块的功能

作者: 你的益达233 | 来源:发表于2020-09-14 16:51 被阅读0次

    Flutter调用Android模块的功能,主要通过MethodChannel

    Flutter端

    const MethodChannel _channel = const MethodChannel('videoUrl');
      try {
        videoUrl = await _channel.invokeMethod<String>("getVideoUrl", {
          "base64Str": video_detail_model.data.courseUrl
        });
      } catch (e) {
        print(e.message);
      }
    

    Android端

    public class MainActivity extends FlutterActivity {
    
    private static final String channel = "videoUrl";
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        new MethodChannel(getFlutterEngine().getDartExecutor().getBinaryMessenger(),channel).setMethodCallHandler(
                new MethodChannel.MethodCallHandler() {
                    @Override
                    public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
    
                        if (methodCall.method.equals("getVideoUrl")) {
                            result.success(getVideoUrl(methodCall.argument("base64Str")));
                        } else {
                            result.notImplemented();
                        }
                    }
                }
        );
        
    
    }
    
    
    
    private String getVideoUrl(String base64Str){
        return Base64Util.getUrl(base64Str,this);
    }
    
    }
    

    关键点channel名字要一样

    相关文章

      网友评论

        本文标题:Flutter调用Android模块的功能

        本文链接:https://www.haomeiwen.com/subject/uzvzektx.html