FlutterActivity MethodChannel和FlutterView

时间:2019-12-13 13:25:08

标签: android kotlin flutter

因此,大约4个月前,我写了Flutter应用程序。现在,我想做一个小小的更改,但是我不能再编译应用程序了,因为GeneratedPluginRegistrant.registerWith(this)不再起作用了,我没有只将Dart代码更改为Kotlin代码。

“ GeneratedPluginRegistrant.registerWith( this )”中的“ this ”向我显示此错误:

Type mismatch.    
Required: FlutterEngine!    
Found: MainActivity

MainActivity类:

import android.os.Bundle
import io.flutter.app.FlutterActivity
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugins.GeneratedPluginRegistrant
import io.flutter.view.FlutterMain

class MainActivity : FlutterActivity() {

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    GeneratedPluginRegistrant.registerWith(this) // here is the error: Type mismatch. Required: FlutterEngine! Found: MainActivity

    MethodChannel(flutterView, CHANNEL).setMethodCallHandler { call, result ->
      if (call.method == "helloFromNativeCode") {
        val greetings = helloFromNativeCode()
        result.success(greetings)
      }
    }
  }

  private fun helloFromNativeCode(): String {
    return "Hello from Native Android Code"
  }

  companion object {
    private const val CHANNEL = "flutter.native/helper"
  }
}

如果正在使用:

import io.flutter.embedding.android.FlutterActivity

代替

import io.flutter.app.FlutterActivity

我可以使用

override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine);
    }

但遇到以下问题:

MethodChannel(flutterView, CHANNEL).setMethodCallHandler { call, result ->
      if (call.method == "helloFromNativeCode") {
        val greetings = helloFromNativeCode()
        result.success(greetings)
      }
    }

因为我在 flutterView 上遇到错误:

Unresolved reference: flutterView

代码如下:

import android.os.Bundle
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugins.GeneratedPluginRegistrant
import io.flutter.view.FlutterMain

class MainActivity : FlutterActivity() {
    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine);
    }

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    MethodChannel(flutterView, CHANNEL).setMethodCallHandler { call, result -> // here is the error
      if (call.method == "helloFromNativeCode") {
        val greetings = helloFromNativeCode()
        result.success(greetings)
      }
    }
  }

  private fun helloFromNativeCode(): String {
    return "Hello from Native Android Code"
  }

  companion object {
    private const val CHANNEL = "flutter.native/helper"
  }
}

我希望有人能帮助我。

4 个答案:

答案 0 :(得分:3)

您应该使用

import io.flutter.embedding.android.FlutterActivity;

并在

中声明您的patformChannel
     @Override
  public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
    GeneratedPluginRegistrant.registerWith(flutterEngine);
    new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
        .setMethodCallHandler(
          (call, result) -> {
            // Note: this method is invoked on the main thread.
            // TODO
          }
        );
  }

有关更多信息,您可以查看文档:{​​{3}}

答案 1 :(得分:2)

使用flutterEngine.getDartExecutor()代替flutterView。

答案 2 :(得分:0)

我花了几天时间试图弄清楚如何将Flutter UI添加到我现有的Android App中。最大的挑战是要使MethodChannel与MainActivity调用的FlutterActivity一起使用。我知道这与这里提出的问题略有不同,但是当我搜索“ Android FlutterActivity MethodChannel”时返回了此帖子。在花了很多关于如何执行此操作的资源之后,我终于在这里找到我的解决方案:  https://github.com/flutter/samples/tree/master/add_to_app/android_using_plugin/app/src/main/java/dev/flutter/example/androidusingplugin

最初,在Android Studio中,打开现有应用程序,我点击了File,New,New Module,Flutter Module。我收到错误消息,必须执行手动步骤。

我的目标是在MainActivity-onCreate中启动FlutterActivity(在flutter_module中打开main.dart),然后利用MethodChannel在有限的平台调用下,利用尽可能多的本机Flutter代码开发Flutter“屏幕”。在开发替换的Flutter代码时,我将继续注释掉现有的Android代码。

这终于对我有用:

../ App_Project / Android / Existing_Android_App / settings.gradle

include ':app'
setBinding(new Binding([gradle: this]))
evaluate(new File(settingsDir.parentFile, '../flutter_module/.android/include_flutter.groovy'))
include ':flutter_module’
project(':flutter_module’).projectDir = new File('../../flutter_module’)
rootProject.name=‘existing_android_app’

../ App_Project / Android / Existing_Android_App / app / build.gradle

dependencies {
…
    implementation project(':flutter')
}

../ App_Project / Android / Existing_Android_App / app / src / main / AndroidManifest.xml

<activity
    android:name="io.flutter.embedding.android.FlutterActivity"
    android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
    android:hardwareAccelerated="true"
    android:windowSoftInputMode="adjustResize" />

../ App_Project / Android / Existing_Android_App / app / src / main / java / com / existing_android_app / MainActivity.java

package com.existing_android_app;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.FlutterEngineCache;
import io.flutter.embedding.engine.dart.DartExecutor;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

public class MainActivity extends AppCompatActivity {

    final String ENGINE_ID = "1";

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        FlutterEngine flutterEngine = new FlutterEngine(this);
        flutterEngine.getDartExecutor().executeDartEntrypoint(DartExecutor.DartEntrypoint.createDefault());

        FlutterEngineCache.getInstance().put(ENGINE_ID, flutterEngine);

        MethodChannel channel = new MethodChannel(flutterEngine.getDartExecutor(), "com.existing_android_app/myMethodChannel");

        channel.setMethodCallHandler(
                new MethodChannel.MethodCallHandler() {
                    @Override
                    public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
                        String url = call.argument("url");
                        if (call.method.equals("openBrowser")) {
                            openBrowser(url);
                        } 
                          else {
                            result.notImplemented();
                        }
                    }
                });

        startActivity(FlutterActivity.withCachedEngine(ENGINE_ID).build(this));
    }

    void openBrowser(String url) {

        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(url));

        this.startActivity(intent);
    }
}

../ App_Project / flutter_module / lib / home_page.dart

class AppHomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<AppHomePage> {

  static const platform = const MethodChannel(‘com.existing_android_app/myMethodChannel’);

  Future<void> _openBrowser() async {
    try {
      final int result = await platform.invokeMethod('openBrowser', <String, String> { 'url': "http://bing.com” });
    }
    catch (e) {
      print('***** _openBrowser error: ' + e.toString());
    }
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        appBar: CustomAppBar(),
        body: Column(
          children: <Widget>[
            RaisedButton(
              label: Text('Search',
                style: TextStyle(fontSize: 18.0),
              ),
              onPressed: () {  _openBrowser(); },
            ) // RaisedButton.icon
          ], // Widget
        ) // Column
      ) // Scaffold
    ); // SafeArea
  }

答案 3 :(得分:0)

为了传递颤振引擎值,我们可以使用 provideFlutterEngine(this) 方法。对于 flutterView,我们可以使用 flutterEngine.dartExecutor。这是代码片段的网址,之前已经回答过 https://stackoverflow.com/a/67698834/11887774 .