Skip to content

Instantly share code, notes, and snippets.

@EaseTheWorld
EaseTheWorld / AndroidPromise.js
Last active August 1, 2024 08:10
Simple implementation of AndroidPromise(Run android async work from javascript using ES6 Promise/async/await)
var AndroidPromise = {
callbacks: {},
requestId: 0,
request: function (work, params={}) {
return new Promise((resolve, reject) => {
let requestId = this.requestId++
this.callbacks[requestId] = {
'resolve': resolve,
'reject': reject
}
@pathnirvana
pathnirvana / AsyncJava.js
Last active July 29, 2024 08:32
Async Await call to JavascriptInterface Android Java WebView
// Allows to call a Android java function asynchronously
// spawn long running computations/io on the Java/Android without blocking the JS/Website running inside the WebView
// Eg. const result = await callAndroidAsync('javaFunction', { param1: 'value1', param2: 'value2' })
// Please give a star if you find this useful
export async function callAndroidAsync(javaFuncName, params) {
const rand = 'asyncJava_' + Math.floor(Math.random() * 1000000)
window[rand] = {}
// func called from android
@timfreiheit
timfreiheit / JSPromise
Last active October 23, 2022 12:26
Android JavascriptInterface Promise
import android.os.Build;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import com.google.gson.Gson;
import java.util.concurrent.Callable;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;