SpeedOf.Me API - Android Integration

This example demonstrates how to integrate the SpeedOf.Me speed test API into an Android app using WebView.

How It Works

The SpeedOf.Me API is JavaScript-based, so Android apps use WebView to:

  1. Load an HTML page containing the speed test
  2. Receive results via @JavascriptInterface

Files

Prerequisites

Quick Start

1. Add Files

2. Configure API Credentials

SomApi.account = "YOUR_API_KEY";
SomApi.domainName = "your-domain.com";

3. Add Internet Permission

<uses-permission android:name="android.permission.INTERNET" />

Architecture

Android App
   └── SpeedTestActivity (Compose)
           └── WebView
                   └── speedtest.html
                           └── SomApi.js
                                   ▼
                   window.Android.onMessage(json)
                                   ▼
                   @JavascriptInterface onMessage()

JavaScript-to-Kotlin Communication

The HTML page sends messages via:

window.Android.onMessage(JSON.stringify({
    type: 'completed',
    data: result
}));

Kotlin receives them via @JavascriptInterface:

class SpeedTestJsInterface(private val viewModel: SpeedTestViewModel) {
    @JavascriptInterface
    fun onMessage(json: String) {
        viewModel.handleMessage(json)
    }
}

// Add to WebView
webView.addJavascriptInterface(
    SpeedTestJsInterface(viewModel),
    "Android"  // This becomes window.Android in JS
)

Loading Options

Option 1: Assets (Offline)

webView.loadUrl("file:///android_asset/speedtest.html")

Option 2: Remote Server

webView.loadUrl("https://your-domain.com/speedtest.html")

WebView Configuration

webView.settings.apply {
    javaScriptEnabled = true      // Required
    domStorageEnabled = true      // Recommended
    mixedContentMode = WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE
}

ProGuard Rules

-keepclassmembers class * {
    @android.webkit.JavascriptInterface <methods>;
}

Production Considerations

  1. API Key Security: Store keys in local.properties or BuildConfig
  2. Thread Safety: @JavascriptInterface methods run on a background thread
  3. Memory Management: Call webView.destroy() in onDestroy()
  4. SSL/TLS: Modern Android requires HTTPS

Troubleshooting

Links