当前位置: 首页 > news >正文

Android geckoview 集成,JS交互,官方demo

官方集成说明

https://firefox-source-docs.mozilla.org/mobile/android/geckoview/consumer/geckoview-quick-start.html

官方JS交互说明

https://firefox-source-docs.mozilla.org/mobile/android/geckoview/consumer/web-extensions.html

官方集成/JS交互实例代码

/* This Source Code Form is subject to the terms of the Mozilla Public* License, v. 2.0. If a copy of the MPL was not distributed with this* file, You can obtain one at http://mozilla.org/MPL/2.0/. */package org.mozilla.geckoview.example.messaging;import android.os.Bundle;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import org.json.JSONException;
import org.json.JSONObject;
import org.mozilla.gecko.util.ThreadUtils;
import org.mozilla.geckoview.GeckoResult;
import org.mozilla.geckoview.GeckoRuntime;
import org.mozilla.geckoview.GeckoRuntimeSettings;
import org.mozilla.geckoview.GeckoSession;
import org.mozilla.geckoview.GeckoView;
import org.mozilla.geckoview.WebExtension;// The apk file for this example can be build using the following mach command:
//
//     mach gradle messaging_example:build
//
// Formatting error reported by Spotless as part of the build can be autofixed
// can be fixed using the mach command:
//
//     mach gradle messaging_example:spotlessApply
//
// After the gradle task messaging_example:build runs successfully, an apk
// file will be stored in the OBJDIR/gradle/build/mobile/android/examples/messaging_example.
// subdirectoriespublic class MainActivity extends AppCompatActivity {private static GeckoRuntime sRuntime;private static final String EXTENSION_LOCATION = "resource://android/assets/messaging/";private static final String EXTENSION_ID = "messaging@example.com";// If you make changes to the extension you need to update thisprivate static final String EXTENSION_VERSION = "1.0";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);GeckoView view = findViewById(R.id.geckoview);GeckoSession session = new GeckoSession();if (sRuntime == null) {GeckoRuntimeSettings settings =new GeckoRuntimeSettings.Builder().remoteDebuggingEnabled(true).build();sRuntime = GeckoRuntime.create(this, settings);}WebExtension.MessageDelegate messageDelegate =new WebExtension.MessageDelegate() {@Nullable@Overridepublic GeckoResult<Object> onMessage(final @NonNull String nativeApp,final @NonNull Object message,final @NonNull WebExtension.MessageSender sender) {if (message instanceof JSONObject) {JSONObject json = (JSONObject) message;try {if (json.has("type") && "WPAManifest".equals(json.getString("type"))) {JSONObject manifest = json.getJSONObject("manifest");Log.d("MessageDelegate", "Found WPA manifest: " + manifest);}} catch (JSONException ex) {Log.e("MessageDelegate", "Invalid manifest", ex);}}return null;}};// Let's make sure the extension is installedsRuntime.getWebExtensionController().ensureBuiltIn(EXTENSION_LOCATION, "messaging@example.com").accept(// Set delegate that will receive messages coming from this extension.extension ->ThreadUtils.runOnUiThread(new Runnable() {@Overridepublic void run() {session.getWebExtensionController().setMessageDelegate(extension, messageDelegate, "browser");}}),// Something bad happened, let's log an errore -> Log.e("MessageDelegate", "Error registering extension", e));session.open(sRuntime);view.setSession(session);session.loadUri("https://mobile.twitter.com");}
}

http://www.dtcms.com/a/437999.html

相关文章:

  • 【APK安全】Android 权限校验核心风险与防御指南
  • 单调队列与单调栈
  • 设计与优化Java API:构建高效、可维护的接口
  • Locality Sensitive Hashing (LSH) 详解:高效检测语言语句重复的利器
  • 阿里云网站开发零起步如何做设计师
  • 后端开发基础概念MVC以及Entity,DAO,DO,DTO,VO等概念
  • 七大排序算法的基本原理
  • Gateway-过滤器
  • 科普:Python 中,字典的“动态创建键”特性
  • Java 21 或 JavaFX 打包 exe 之 GraalVM Native Image 方案
  • 1.2.3 MCP(Model Context Protocol)
  • dede网站栏目管理网络科技是做什么的
  • 《Gdb 调试实战指南:不同风格于VS下的一种调试模式》
  • lua虚拟机的垃圾回收机制
  • 网站建设需要的材料wordpress自带的404
  • MPAndroidChart 用法解析和性能优化 - Kotlin Java 双版本
  • Qt中使用日志---Log4Qt
  • linux centos 7 解决终端提示符出现-bash-4.2的问题
  • MCP模型上下文协议实战:个人应用项目如何集成MCP?
  • Vue--Vue基础(一)
  • 外贸网站推广如何做郑佩佩 最新消息
  • 用VScode和msys2配置C/C++和wxWidgets
  • 【升级安卓9教程】华为Q21_Q21A_Q21C_Q21AQ_Q21AE_hi3798mv200线刷烧录包带adb权限
  • vite vue 打包后运行,路由首页加载不出来
  • 《C++ STL:vector类(下)》:攻克 C++ Vector 的迭代器失效陷阱:从源码层面详解原理与解决方案
  • 微软Agent框架深度解析:重新定义AI应用开发的革命性架构
  • 微信看视频打赏网站建设项目立项流程图
  • 爬虫与自动化技术深度解析:从数据采集到智能运维的完整实战指南
  • [工作流节点8] 更新记录节点应用案例 —— 业务自动化的关键一环
  • MySQL表的内连和外连