在Flutter页面中嵌入Activity原生

Anroid端总共四步:

第一步:建一个View页面,实现PlatformView,里面有一个getView方法,一定要有的哦

package com.yinuoapp.login;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;

import com.yinuoapp.smartsist.R;

import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.platform.PlatformView;

public class PhoneLoginView implements PlatformView {

    private MethodChannel channel;
    private View nativeView;

    PhoneLoginView(Context context, MethodChannel channel){
        this.channel=channel;
        nativeView= LayoutInflater.from(context).inflate(R.layout.activity_phone_login,null,false);

    }

    @Override
    public View getView() {
        return nativeView;
    }

    @Override
    public void dispose() {

    }
}

第二步:建一个Factory

package com.yinuoapp.login;

import android.content.Context;

import io.flutter.plugin.common.MessageCodec;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.platform.PlatformView;
import io.flutter.plugin.platform.PlatformViewFactory;

public class PhoneLoginFactory extends PlatformViewFactory {
    private MethodChannel channel;
    private PhoneLoginView view;

    public PhoneLoginFactory(MessageCodec<Object>  createArgsCodec,MethodChannel channel){
        super(createArgsCodec);
        this.channel=channel;
    }

    @Override
    public PlatformView create(Context context, int viewId, Object args) {
        view=new PhoneLoginView(context,channel);
        return view;
    }
}

第三步:建一个Plugin

package com.yinuoapp.login;

import androidx.annotation.NonNull;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.StandardMessageCodec;

public class PhoneLoginPlugin implements FlutterPlugin, MethodChannel.MethodCallHandler {
    static String CHANNEL="com.yinuo/phoneLogin";
    static String VIEW_CHANNEL="com.yinuo/view/phoneLogin";
    private MethodChannel channel;
    private PhoneLoginFactory factory;

    @Override
    public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
        //创建通道
        channel=new MethodChannel(binding.getBinaryMessenger(),CHANNEL);
        //注册通道的监听
        channel.setMethodCallHandler(this);
        //创建platform的工厂对象
        factory=new PhoneLoginFactory(StandardMessageCodec.INSTANCE,channel);
        //在flutter的引擎上注册loginphone的工厂对象
        binding.getPlatformViewRegistry().registerViewFactory(VIEW_CHANNEL,factory);

    }

    @Override
    public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
        channel.setMethodCallHandler(null);
    }

    @Override
    public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
        String name=call.method;
        if(name.equals("prelogin")){

        }
    }
}

第四步:MainActiviy注册

 @Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        super.configureFlutterEngine(flutterEngine);
//        Log.i(TAG,"打开设备时就进入 到了这里");
//        if (Build.VERSION.SDK_INT >= 6.0) {
//            RequestPermissionUtil.request(this, new String[] { Manifest.permission.READ_PHONE_STATE,
//                    Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE });
//        }
        //蓝牙读身份证
        blueCardChannel = new BlueCardChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), this,
                this);
        //软件安装//通用渠道
        new CommonChannel((flutterEngine.getDartExecutor().getBinaryMessenger()),this,this);
        //通知栏调取
//        new NotifierChannel(flutterEngine.getDartExecutor().getBinaryMessenger());
        //视频sdk
        flutterEngine.getPlatformViewsController().getRegistry().registerViewFactory("com.yinuo.getvideo",new VideoViewFactory(flutterEngine.getDartExecutor().getBinaryMessenger()));
        //一键登录
//        new PhoneLoginChannel((flutterEngine.getDartExecutor().getBinaryMessenger()),this,this);
        //一键登录
        flutterEngine.getPlugins().add(new PhoneLoginPlugin());
//        VideoViews videochannel=new VideoViews(flutterEngine.getDartExecutor().getBinaryMessenger(),this.getContext());


    }

Flutter端两步:

第一步:AndoridView

第二步:MethodChannel

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值