Home

Awesome

Pocketbase Android Example

Android example app to run pocketbase from android device using pocketbaseMobile

Setup

add a folder in Project>app>libs and add pocketbaseMobile.aar file from here

import in app level build.gradle

dependencies {
    ...
    implementation fileTree(include: ['*.jar', '*.aar'], dir: 'libs')
}

Usage

Use CoroutineScope to call pocketbase methods ( import kotlin coroutines libraries)

private val uiScope = CoroutineScope(Dispatchers.Main + Job())

To start pocketbase

// use dataPath where app have write access, for example temporary cache path `context.cacheDir.absolutePath`
uiScope.launch {
    withContext(Dispatchers.IO) {
        PocketbaseMobile.startPocketbase(dataPath, hostname, port)
    }
}

To stop pocketbase

uiScope.launch {
    withContext(Dispatchers.IO) {
        PocketbaseMobile.stopPocketbase()
    }
}

To listen pocketbase events, and also handle custom api requests

pocketbaseMobile have two custom routes as well ,/api/nativeGet and /api/nativePost, we can get these routes in this callback and return response from kotlin

PocketbaseMobile.registerNativeBridgeCallback { command, data ->
    this.runOnUiThread {
        // Update ui from here
    }
    // return response back to pocketbase
    "response from native"
}

Screenshot