1

是否可以从我的原生 Android 代码中获取当前活动(如 FooView.java 的构造函数所示,我需要提供一个活动)?

我需要 FooView.java 本身来检查相机权限并检查是否安装了 ARCore。我对 Flutter 和它的平台集成相当陌生,我无法在网上找到答案。如果它是 Flutter 插件,则可以通过该活动访问该活动registrar.activity(),但我没有注册商,因为我采用了非插件方法(我相信它被称为App 注册)。

MainActivity.java

public class MainActivity extends FlutterActivity {
  @Override public void configureFlutterEngine(FlutterEngine flutterEngine) {
    GeneratedPluginRegistrant.registerWith(flutterEngine);
    flutterEngine.getPlatformViewsController().getRegistry().registerViewFactory("fooView", new FooViewFactory());
  }
}

FooView.java

public class FooView implements PlatformView {
  private boolean requestInstall = true;

  FooView(Context context, int id, Map<String, Object> creationParams) {
    Activity someActivity = ?????; // TODO: Find where to get the activity holding the view
    switch (ArCoreApk.getInstance().requestInstall(someActivity, requestInstall)) {
      ...
    }
  }
}
4

1 回答 1

0

首先,将您的 viewFactory 构造函数更改为此以包含活动:

class NativeViewFactory(val activity: Activity)

现在更改本机 View 的构造函数,在您的情况下是FooView. 将其更改为此以包含活动。

FooView(Activity activity,Context context, int id, Map<String, Object> creationParams)

现在最后一部分是替换 MainActivity 中的以下代码:

flutterEngine.getPlatformViewsController().getRegistry().registerViewFactory("fooView", new FooViewFactory());

有了这个:

flutterEngine.getPlatformViewsController().getRegistry().registerViewFactory("fooView", new FooViewFactory(getActivity()));
于 2021-11-09T11:04:20.390 回答