flutter methodchannel调用原生方法,实现原生插件

2 篇文章 0 订阅
2 篇文章 0 订阅

在获取手机电量,屏幕信息等,都需要flutter 调用原生实现,这部分flutter 官方已经帮我们实现好了,对于部分功能,需要自己实现, 步骤如下


# 创建一个 flutter 应用,使用 as 打开 android 目录, MainActivity 代码如下

package com.example.flutter_app;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import java.util.Map;

import io.flutter.app.FlutterActivity;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterActivity {

  private static final String CHANNEL = "demo.gawkat.com/info";

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GeneratedPluginRegistrant.registerWith(this);

    new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(new MethodChannel.MethodCallHandler() {
      @Override
      public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {

        final Map<String, Object> arguments = methodCall.arguments();

        if (methodCall.method.equals("getMessage")) {
           result.success("Android say hi."+ ((String) arguments.get("from")));
        }
//        openWebPage("http://www.bitying.com");
      }
    });
  }

  public void openWebPage(String url) {
    Uri webpage = Uri.parse(url);
    Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
    if (intent.resolveActivity(getPackageManager()) != null) {
      startActivity(intent);
    }

  }
}



# ios 端 appDelegate.m

#include "AppDelegate.h"
#include "GeneratedPluginRegistrant.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  [GeneratedPluginRegistrant registerWithRegistry:self];
  // Override point for customization after application launch.
    
    FlutterViewController* controller = (FlutterViewController*)self.window.rootViewController;

    FlutterMethodChannel* channel = [FlutterMethodChannel
                                     methodChannelWithName:@"demo.gawkat.com/info"
                                     binaryMessenger:controller
                                    ];


    [channel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
        NSString *from = call.arguments[@"from"];

        if([@"getMessage" isEqualToString:call.method]) {
            NSString *messgae = @"IOS says greetings";
            NSString *returnMessage = [messgae stringByAppendingString:from];
            result(returnMessage);
        }
    }];
    

    
  return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

@end


# flutter 端 main.dart 编写如下代码

  static const platform = const MethodChannel('demo.gawkat.com/info');

  String _message = 'No message yet...';

  Future<String> _getMessage() async {
    var sendMap = <String, dynamic> {
     'from' : 'boy',
    };
    String value;
    try {
      value = await platform.invokeMethod('getMessage', sendMap);
    } catch(e) {
      print(e);
    }
    return value;
  }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值