0%

安卓GeckoView的简单使用

创建项目

项目最好是安卓5.0(21)及以上,不然会出现各种各样的问题

引入依赖

引入 GeckoView

注意: 直接在 build.gradle 里面引入可能会有问题,所以我们选择手动下载
进入 Maven Repository: org.mozilla.geckoview » geckoview 下载需要的 GeckoView 版本的 aar 文件
然后放进你的项目的 app/libs 里并引入

1
implementation fileTree(dir: "libs", include: ["*.jar", "*.aar"])

引入 SnakeYAML

最新版可以直接引入最新版本的 YAML, 如果你的 GeckoView 版本较低(如108), YAML 的版本也要低一点(如1.27)

1
implementation "org.yaml:snakeyaml:2.4"

引入 Support-v4 (非必要)

如果出现 SimpleArrayMap 类未找到的问题才需要引入,一般是不需要
并且最好是用19.1.0版本

1
implementation "com.android.support:support-v4:19.1.0"

简单使用

创建 GeckoView 实例

在你的 Layout 文件中创建 GeckoView 或动态创建

1
2
3
4
<org.mozilla.geckoview.GeckoView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/geckoView"/>

初始化 GeckoView

导入包

1
2
3
import org.mozilla.geckoview.GeckoRuntime;
import org.mozilla.geckoview.GeckoSession;
import org.mozilla.geckoview.GeckoView;

创建私有字段

1
2
3
GeckoSession session;
GeckoView gecko;
GeckoRuntime runtime;

初始化

1
2
3
4
5
6
7
session = new GeckoSession();
gecko = findViewById(R.id.geckoview);
runtime = GeckoRuntime.create(this);

session.setContentDelegate(new GeckoSession.ContentDelegate(){});
session.open(runtime);
gecko.setSession(session);

载入网址(Uri)

1
session.loadUri("https://blog.1503dev.top/");

更多用法见参考

参考