Flutter调用Android模块的功能
2020-09-14 本文已影响0人
你的益达233
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名字要一样