An Android event bus for WebView and JS.

Related tags

EventBus drekkar
Overview

Logo

Release

Join the chat at https://gitter.im/coshx/drekkar

An event bus for sending messages between WebView and embedded JS. Alter ego of Caravel.

Features

  • Easy, fast and reliable event bus system
  • Multiple bus support
  • Multithreading support
  • WebView ~> JavaScript supported types:
    • Bool
    • Int
    • Float
    • Double
    • String
    • Any list (using types in this list, including maps)
    • Any map (using types in this list, including lists)
  • JavaScript ~> Android supported types:
    • Boolean
    • Int
    • Float (available as a Double)
    • String
    • Array (available as a List)
    • Object (available as a Map)

Installation

Using JitPack

Merge this code into your root build.gradle file:

allprojects {
	repositories {
		maven { url "https://jitpack.io" }
	}
}

Then, add this dependency to your module:

dependencies {
    compile 'com.github.coshx:drekkar:v0.1.1'
}

Finally, you need to load the internal JS script Drekkar is using to make magic happen. You can either use R.draw.drekkar_min or add the minified JS script from the latest release to your raw resources. This script must be loaded in any webpage you are using Drekkar.

Using as a submodule

Clone this repo and add the drekkar module to your workspace.

Get started

Drekkar allows developers to communicate between their WebView and the embedded JS. You can send any kind of message between these two folks.

Have a glance at this super simple sample. Let's start with the Android part:

class MyActivity extends Activity {
    WebView webView;

    void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        webView = (WebView) findViewById(R.id.my_webview_id);

        // Prepare your bus before loading your web view's content
        Drekkar.getDefault(this, webView, new WhenReady() {
            @Override
            public void run(EventBus bus) {
                // In this scope, the JS endpoint is ready to handle any event.
                // Register and post your events here
                List<Integer> list = new ArrayList<>();
                list.add(1);
                list.add(2);
                list.add(3);

                bus.post("MyEvent", list);

                MyActivity.this.bus = bus; // You can save your bus for firing events later
            }
        });

        // ... Load web view's content there
    }
}

And now, in your JS:

var bus = Drekkar.getDefault();

bus.register("AnEventWithAString", function(name, data) {
    alert('I received this string: ' + data);
    bus.post("AnEventForAndroid");
});

And voilà!

Porting your app from Caravel to Drekkar

Super duper easy. Just use the same codebase and use the JS script from Drekkar. Finally, add this after having loaded the Drekkar script:

var Caravel = Drekkar;

Troubleshooting

😕 Sometimes the bus is not working?!

Firstly, ensure you are using the bus correctly. Check if you are unregistering the bus when exiting the controller owning your web component. Use the unregister method for this.

Drekkar automatically cleans up any unused bus when you create a new one. However, this operation is run in the background to avoid any delay on your side. So, a thread collision might happen if you have not unsubscribed your bus properly.

However, if you think everything is good with your codebase, feel free to open a ticket.

What object should I use as a subscriber?

A subscriber could be any object except the watched target. We recommend to use the activity/current context as a subscriber (it is a common pattern).

Reserved names

DrekkarInit is an internal event, sent by the JS part for running the WhenReady object.

Also, the default bus is named default. If you use this name for a custom bus, Drekkar will automatically switch to the default one.

Finally, Drekkar names its JS interface DrekkarWebViewJSEndpoint.

Keep in mind event and bus names are case-sensitive.

You might also like...
Bus Scheduler - The source code for the Bus Scheduler app codelab

Bus Scheduler App This folder contains the source code for the Bus Scheduler app

Event bus for Android and Java that simplifies communication between Activities, Fragments, Threads, Services, etc. Less code, better quality.
Event bus for Android and Java that simplifies communication between Activities, Fragments, Threads, Services, etc. Less code, better quality.

EventBus EventBus is a publish/subscribe event bus for Android and Java. EventBus... simplifies the communication between components decouples event s

Android library listening network connection state and change of the WiFi signal strength with event bus

NetworkEvents Android library listening network connection state and change of the WiFi signal strength with event bus. It works with any implementati

An enhanced Guava-based event bus with emphasis on Android support.

Otto - An event bus by Square An enhanced Guava-based event bus with emphasis on Android support. Otto is an event bus designed to decouple different

Event Bus powered by Kotlin Coroutines Flow

FlowBus FlowBus is a kotlin event bus implementation. Powered by Kotlin Coroutines and Flows Android MainThread-aware Fully operable from Java code Ex

This prototype app provides a list of events to be held under an organization (school, college, club, etc.) and the users can manually set event reminders at their scheduled time so that they do not miss an event.

E-CELL NITS Sample App This prototype app provides a list of events to be held under E-Cell NIT Silchar (for example, Srijan 2.0) and the users can ma

Event pattern & event properties framework

Renetik Android - Event & Property https://github.com/renetik/renetik-android-event Documentation Framework to enjoy, improve and speed up your applic

A simple library for parsing and playing links from YouTube, YouTube Music, Vimeo and Rutube is WebView without the need to connect api data services. Request caching is available now
A simple library for parsing and playing links from YouTube, YouTube Music, Vimeo and Rutube is WebView without the need to connect api data services. Request caching is available now

Android Oembed Video A simple library for parsing and playing links from YouTube, YouTube Music, Vimeo and Rutube and others in the WebView without th

Okuki is a simple, hierarchical navigation bus and back stack for Android, with optional Rx bindings, and Toothpick DI integration.

Okuki A simple, hierarchical navigation bus and back stack for Android, with optional Rx bindings, and Toothpick integration for automatic dependency-

This Android app shows bus connections from Koleje Strahov station to Dejvická station and the other way in the city of Prague
This Android app shows bus connections from Koleje Strahov station to Dejvická station and the other way in the city of Prague

This Android app shows bus connections from Koleje Strahov station to Dejvická station and the other way in the city of Prague. These are important for many students from the Czech Technical University in Prague.

SeatBookView is an Android Studio Library that helps to make it easier to create Bus, Train, Cinema Theater Seat UI and all functionalities are given.
SeatBookView is an Android Studio Library that helps to make it easier to create Bus, Train, Cinema Theater Seat UI and all functionalities are given.

SeatBookView SeatBookView is an Android Studio Library that helps to make it easier to create Bus 🚍 , Train 🚉 , Cinema Theater Seat UI and all funct

Proof of concept Android WebView implementation based on Chromium code

Deprecation Notice This project is un-maintained. The recommended alternative is the Crosswalk Project. I did not have the time to keep the project up

Android WebView wrapper based on chromium

ChromiumWebView Android WebView wrapper based on chromium Notice This is just a experimental project, don't use it in product. If you have problem whe

Android 中 WebView 与原生通信

JsBridge SDK支持 Js调用原生方法并支持异步回调和同步回调 原生调用Js方法并支持异步回调 Js调用名称空间可自由配置,统一管理命名空间 支持Js调用原生方法多次回调,如果不想多次回调可以删除回调方法 支持部分Js框架中window并非顶级window API介绍 callJsFunct

android webview loader using ksp

KSPWebViewLoader 🚧 @WebViewBuilder Annotation can be automating your webview settings. (WIP) How to use @WebViewBuilder( url = "https://www.googl

android webview choose file to upload
android webview choose file to upload

Android WebView文件上传 本工程为大家演示在H5中如何上传文件,同时也提供了服务端代码,有兴趣的读者可以部署起来,测试文件上传功能。 如果只对客户端部分感兴趣,只需要关注WebView文件选择部分即可 服务端代码下载地址, 客户端可以用如下url来上传文件 http://your ip

Demonstration of calling requestFocusFromTouch() in WebView is changing colors in Android app
Demonstration of calling requestFocusFromTouch() in WebView is changing colors in Android app

webview-changing-colors-bug Demonstration of calling requestFocusFromTouch() in WebView is changing colors in Android app This is the problematic code

A simple Android webbrowser based on webview.
A simple Android webbrowser based on webview.

"FOSS Browser" is a fully free (as in freedom) open source Android browser: privacy-friendly with a modern user interface. - Your privacy - your data.

Web Container: A simple web container library for Android to help fellow developer to open WebView easily

WebContainer Description Web Container is a simple web container library for And

Comments
Releases(v0.1.2)
Event bus for Android and Java that simplifies communication between Activities, Fragments, Threads, Services, etc. Less code, better quality.

EventBus EventBus is a publish/subscribe event bus for Android and Java. EventBus... simplifies the communication between components decouples event s

Markus Junginger 24.2k Jan 7, 2023
An enhanced Guava-based event bus with emphasis on Android support.

Otto - An event bus by Square An enhanced Guava-based event bus with emphasis on Android support. Otto is an event bus designed to decouple different

Square 5.2k Jan 9, 2023
Event pattern & event properties framework

Renetik Android - Event & Property https://github.com/renetik/renetik-android-event Documentation Framework to enjoy, improve and speed up your applic

Renetik 2 Nov 2, 2022
The source code for the Bus Scheduler app codelab

Bus Scheduler App This folder contains the source code for the Bus Scheduler app codelab. Introduction The Bus Scheduler app displays a list of bus st

null 0 Nov 8, 2021
EventBus for Android,消息总线,基于SharedFlow,具有生命周期感知能力,支持Sticky,支持线程切换,支持延迟发送。

背景 跨页面通信是一个比较常见的场景,通常我们会选择使用EventBus,但EventBus无法感知声明周期,收到消息就会回调,所以有了LiveData之后很快就有了LiveEventBus。不过它也有缺点,比如不能切换线程。

BiuBiuQiu0 167 Jan 3, 2023
A lightweight eventbus library for android, simplifies communication between Activities, Fragments, Threads, Services, etc.

AndroidEventBus This is an EventBus library for Android. It simplifies the communication between Activities, Fragments, Threads, Services, etc. and lo

Mr.Simple 1.6k Nov 30, 2022
PubSub - 使用 Kotlin Coroutines 实现的 Local Pub/Sub、Event Bus、Message Bus

PubSub 使用 Kotlin Coroutines 实现的 Local Pub/Sub、Event Bus、Message Bus 下载 将它添加到项目的

Tony Shen 4 May 30, 2022
MarkdownView is an Android webview with the capablity of loading Markdown text or file and display it as HTML, it uses MarkdownJ and extends Android webview.

About MarkdownView (Markdown For Android) is an Android library that helps you display Markdown text or files (local/remote) as formatted HTML, and st

Feras Alnatsheh 1k Dec 20, 2022
MarkdownView is an Android webview with the capablity of loading Markdown text or file and display it as HTML, it uses MarkdownJ and extends Android webview.

MarkdownView is an Android webview with the capablity of loading Markdown text or file and display it as HTML, it uses MarkdownJ and extends Android webview.

Feras Alnatsheh 1k Dec 20, 2022
A Kotlin binding to webview, a tiny cross-platform webview library, supports Java and Native.

webviewko provides a Kotlin/JVM and a Kotlin/Native(experimental) binding to webview, a tiny cross-platform webview library to build modern cross-platform GUIs using WebView2, WebKit and WebKitGTK.

Winterreisender 17 Dec 30, 2022