HTML to APK Builder

ANDROIDAPI REFERENCE

AndroidAPI: device info, toast, vibration, screen and torch

Reference for showToast, getDeviceInfo, getAppInfo, battery, network state, vibration patterns, screen brightness, keep-screen-on, torch and deep links.

Updated:

These methods read basic device state or give the user immediate feedback. Most are synchronous: they return a value at once and send no event. None of them is Pro-gated. Read how the bridge works first if you have not used AndroidAPI before.

Method Returns / event Permission Pro
showToast nothing none no
getDeviceInfo JSON string none no
getAppInfo JSON string none no
getBatteryLevel number none no
isOnline, getNetworkType boolean, string ACCESS_NETWORK_STATE no
vibrate, cancelVibrate nothing VIBRATE no
vibratePattern vibrateResult VIBRATE no
getScreenBrightness number none no
setScreenBrightness, keepScreenOn screenSettingResult none no
setTorch torchResult CAMERA no
getLaunchDeepLink, window.onDeepLink string none no

showToast

AndroidAPI.showToast(message)

Shows a short native Android message at the bottom of the screen. It returns nothing and sends no event. Use it for brief confirmations such as “Saved”. Show important information inside your page instead, because a toast disappears after a few seconds.

function toast(text) {
  if (typeof AndroidAPI !== 'undefined') AndroidAPI.showToast(text);
  else console.log(text); // browser fallback
}
toast('Settings saved');

getDeviceInfo

AndroidAPI.getDeviceInfo() // → JSON string

Returns a JSON string with model, manufacturer, androidVersion (for example "14") and sdk (the API level, for example 34). Parse it before use.

const device = JSON.parse(AndroidAPI.getDeviceInfo());
document.querySelector('#device').textContent =
  `${device.manufacturer} ${device.model}, Android ${device.androidVersion} (API ${device.sdk})`;

getAppInfo

AndroidAPI.getAppInfo() // → JSON string

Returns packageName, versionName, versionCode, appName, isPro, deviceSdk and targetSdk. Show the version in an “About” screen, or check isPro before offering a Pro-gated feature so you can hide it instead of failing.

const app = JSON.parse(AndroidAPI.getAppInfo());
document.querySelector('#version').textContent = `${app.appName} ${app.versionName}`;
if (!app.isPro) document.querySelector('#gps-button').hidden = true;

isPro describes how the app was built. It is not a subscription system for the people who use your app.

getBatteryLevel

AndroidAPI.getBatteryLevel() // → integer 0–100, or -1

Returns the battery percentage. -1 means the value is unavailable. There is no event for battery changes; read the value again when you need it.

const level = AndroidAPI.getBatteryLevel();
label.textContent = level < 0 ? 'Battery: unknown' : `Battery: ${level}%`;

isOnline and getNetworkType

AndroidAPI.isOnline()       // → true / false
AndroidAPI.getNetworkType() // → "wifi" | "cellular" | "ethernet" | "none" | "unknown"

Report the current connection. They need ACCESS_NETWORK_STATE. Quick Build and URL projects include it; in Advanced Build the permission step detects these calls and lets you add it with one tap. A connection does not guarantee that a server is reachable, so still handle failed requests.

if (!AndroidAPI.isOnline()) {
  showBanner('You are offline. Changes are saved on this device.');
} else if (AndroidAPI.getNetworkType() === 'cellular') {
  showBanner('Large downloads may use mobile data.');
}

In plain browser code, navigator.onLine and the online/offline events also work.

vibrate and cancelVibrate

AndroidAPI.vibrate(milliseconds)
AndroidAPI.cancelVibrate()

vibrate(200) vibrates once for 200 ms. cancelVibrate() stops any vibration. Neither sends an event. They need the VIBRATE permission, which has no runtime dialog. Quick Build includes it; in Advanced Build the permission step detects the call.

button.addEventListener('click', () => AndroidAPI.vibrate(40)); // short tap feedback

vibratePattern

AndroidAPI.vibratePattern(patternJson, repeat)

Plays a pattern of off/on durations in milliseconds. patternJson is a JSON array string with at most 32 steps. The first value is a pause. repeat is -1 for no repeat, or the index to loop from.

Result event: vibrateResult {success, steps, repeat, error}. Errors: invalid_pattern (bad JSON, negative value, more than 32 steps), no_vibrator.

window.addEventListener('vibrateResult', e => {
  if (!e.detail.success) console.log('Vibration failed:', e.detail.error);
});
// pause 0 ms, buzz 100, pause 80, buzz 100
AndroidAPI.vibratePattern(JSON.stringify([0, 100, 80, 100]), -1);

Always call cancelVibrate() when a looping pattern should stop, for example when the user leaves the screen.

getScreenBrightness

AndroidAPI.getScreenBrightness() // → integer 0–255

Returns the system brightness setting. It does not reflect a value you set with setScreenBrightness.

setScreenBrightness

AndroidAPI.setScreenBrightness(level) // 0.0–1.0, or -1 to restore

Changes the brightness of your app’s window only, for example to show a QR code at full brightness. The system setting is not changed and no special permission is needed. Pass -1 to return to the user’s normal brightness.

Result event: screenSettingResult {setting, value, success, error}.

function showTicket() {
  AndroidAPI.setScreenBrightness(1.0);
}
function hideTicket() {
  AndroidAPI.setScreenBrightness(-1);
}

keepScreenOn

AndroidAPI.keepScreenOn(enable) // true / false

Prevents the screen from turning off while your app is visible. Use it for recipes, timers, games or presentations, and turn it off when the task ends to save battery. Result event: screenSettingResult.

startButton.onclick = () => AndroidAPI.keepScreenOn(true);
stopButton.onclick = () => AndroidAPI.keepScreenOn(false);

setTorch

AndroidAPI.setTorch(enabled) // true / false

Turns the camera flash LED on or off as a continuous torch. It needs the CAMERA permission: select it in the project and the app asks for it the first time. It can fail when the device has no flash or another app uses the camera.

Result event: torchResult {enabled, success, error}. Errors include permission_denied and hardware failures.

let torchOn = false;
window.addEventListener('torchResult', e => {
  const r = e.detail;
  if (r.success) { torchOn = r.enabled; button.textContent = torchOn ? 'Turn off' : 'Turn on'; }
  else alert('Torch unavailable: ' + r.error);
});
button.onclick = () => AndroidAPI.setTorch(!torchOn);
AndroidAPI.getLaunchDeepLink()        // → URI string or ""
window.onDeepLink = function (uri) {} // links that arrive later

When another app or a link opens your app, getLaunchDeepLink() returns the URI that launched it. Links that arrive while the app is already open call window.onDeepLink. Validate the URI before using it. These methods only read links; they do not register new link types.

function route(uri) {
  if (!uri) return;
  const url = new URL(uri);
  const id = url.searchParams.get('item');
  if (id && /^\d+$/.test(id)) openItem(id);
}
route(AndroidAPI.getLaunchDeepLink());
window.onDeepLink = route;