flutter methodchannel調用原生方法,實現原生插件

在獲取手機電量,屏幕信息等,都需要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;
  }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章