Android and TV
Add the SDK to an Android app for phones, tablets, Android TV or Fire TV, in Kotlin or Java. Before you start, get your account approved and create an SDK key (steps 1 and 2 of the overview).
| Download | Android & TV package from your SDK page (approved accounts only) |
| In the zip | idlepay.aar (the SDK), IdlePay.kt (a small helper you add to your sources), idlepay-sources.jar (optional, for your IDE) |
| Requires | minSdk 24 (Android 7.0) or later. The Kotlin Android plugin, also in Java apps, to compile IdlePay.kt. |
| CPU | arm64-v8a, armeabi-v7a, x86_64 and x86. That's about 7 MB per architecture: publish an App Bundle so each device downloads only its own. |
1. Add the files to your project
- Copy
idlepay.aartoapp/libs/. - Copy
IdlePay.ktto your sources, for exampleapp/src/main/java/io/idlepay/IdlePay.kt. It's in the packageio.idlepay. - Add the AAR to
app/build.gradle.kts:
android {
defaultConfig {
minSdk = 24
}
}
dependencies {
implementation(files("libs/idlepay.aar"))
}
With Groovy (build.gradle): implementation files('libs/idlepay.aar'). Java-only app? Add the Kotlin Android plugin (org.jetbrains.kotlin.android) to the app module so IdlePay.kt compiles. You don't need to write any Kotlin yourself.
Add the two permissions to AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Release builds need no extra R8 or ProGuard rules: the AAR brings its own.
2. Ask the user, and remember the answer
The helper has a ready-made consent dialog. It shows IdlePay's standard wording with your app's name, saves the answer on the device, and counts dismissing it as "No thanks". Show it once, for example from your main activity:
// At the top level of a file:
const val SDK_KEY = "ipsk_..." // from the SDK page of your dashboard
// In your main activity's onCreate:
if (IdlePay.consent(this) == null) { // never asked yet
IdlePay.askConsent(this, "My App", reward = "you use My App without ads") { agreed ->
if (agreed) IdlePay.start(this, SDK_KEY, consent = true)
}
}
reward is optional: it finishes the sentence "In return, …". Leave it out if you don't offer anything for sharing.
The dialog's wording is in English. To translate it or use your own words, pass title, message, allow and decline, for example from your string resources. Your text must still cover what the consent screen must say:
IdlePay.askConsent(this, "My App",
title = getString(R.string.sharing_title),
message = getString(R.string.sharing_message),
allow = getString(R.string.sharing_allow),
decline = getString(R.string.sharing_decline)) { agreed ->
if (agreed) IdlePay.start(this, SDK_KEY, consent = true)
}
Add a switch in your settings screen. Turning it off calls IdlePay.setConsent(context, false), which also stops sharing. Turning it on calls IdlePay.askConsent again.
Your own consent screen instead? That's fine if it follows the consent rules. Save its answer with IdlePay.setConsent(context, agreed), and the rest of this guide stays the same.
3. Start it on every launch
Once the user has agreed, start the SDK each time your app's process starts. The best place is your Application class, which runs once per process:
class MyApp : Application() {
override fun onCreate() {
super.onCreate()
if (IdlePay.consent(this) == true) {
IdlePay.start(this, SDK_KEY, consent = true)
}
}
}
Register it in the manifest with <application android:name=".MyApp" …> if you don't have one yet. Calling start while the SDK is already running throws "already started", so start it in one place only (here, and after the user says yes).
What the helper does for you:
- shares on Wi-Fi and Ethernet only, and pauses on mobile data;
- reconnects as soon as the network changes;
- keeps its device token in
noBackupFilesDir, so a backup restored on another phone doesn't copy it; - identifies the install by its Android ID and your package name, so reinstalling the app keeps the same device.
To cap traffic per device, pass a daily limit in MB: IdlePay.start(context, key, consent = true, maxDailyMB = 500). Sharing then pauses until midnight UTC once the device has shared that much.
4. Check that it works
On a real phone or TV box on home Wi-Fi, read the status, for example from a debug screen or a log line:
val s = IdlePay.status()
Log.i("IdlePay", "${s.state}: ${s.message} (device ${s.deviceID}, IP ${s.publicIP})")
Within about a minute, state should be online, and the device appears on your SDK page. Anything else comes with a message that says why: look it up in the messages. To see the SDK's own log while you test:
io.idlepay.sdk.Sdk.setLogger(object : io.idlepay.sdk.Logger {
override fun log(line: String) { Log.d("IdlePay", line) }
})
Android emulators aren't real users: test on a real device. See testing for what to expect.
5. Pause and stop
IdlePay.setPaused(true) // your own reason: battery saver, a video call... IdlePay.setPaused(false) // resume (it stays paused on mobile data) IdlePay.stop(context) // the user turned sharing off
6. Keep sharing in the background
The SDK runs as long as your app's process is alive. When the user leaves the app, Android may stop the process within minutes, and sharing stops with it. That's fine: the device simply earns while the app runs. To keep sharing in the background, run a foreground service with a visible notification that says the connection is being shared:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<service
android:name=".SharingService"
android:exported="false"
android:foregroundServiceType="specialUse">
<property
android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
android:value="Shares the device's internet connection with IdlePay while the user allows it" />
</service>
class SharingService : Service() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
NotificationManagerCompat.from(this).createNotificationChannel(
NotificationChannelCompat.Builder("sharing", NotificationManagerCompat.IMPORTANCE_LOW)
.setName("Connection sharing").build())
val notification = NotificationCompat.Builder(this, "sharing")
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("Sharing your connection")
.setContentText("Turn it off any time in Settings")
.setOngoing(true)
.build()
ServiceCompat.startForeground(this, 1, notification,
if (Build.VERSION.SDK_INT >= 34) ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE else 0)
return START_STICKY
}
override fun onBind(intent: Intent?): IBinder? = null
}
- Start the service from your activity after the user allowed sharing, with
ContextCompat.startForegroundService(context, Intent(context, SharingService::class.java)). Stop it withstopServicewhen the user turns sharing off. - The SDK itself still starts in your
Applicationclass, as in step 3. The service only keeps the process alive. - On Android 13 and later, ask for the notification permission first. On Android 14 and later, Google Play asks you to declare the special-use foreground service in the Play Console and explain it.
Android TV and Fire TV
- The same AAR and helper work on TV. TV boxes are usually on Ethernet or Wi-Fi and stay on for hours, so they make good devices.
- The consent dialog works with a remote: its buttons are reached with the D-pad. If you build your own screen, make it just as easy.
- Fire TV apps follow the Amazon Appstore's rules: mention the sharing in the listing and privacy policy there too.
From Java
IdlePay.askConsent(activity, "My App", agreed -> {
if (agreed) IdlePay.start(activity, "ipsk_...", true);
});
io.idlepay.sdk.State s = IdlePay.status();
Log.i("IdlePay", s.getState() + ": " + s.getMessage());
IdlePay.setPaused(true);
IdlePay.stop(context);
Updating the SDK
Replace idlepay.aar and IdlePay.kt with the files from the new version and release your app. Devices keep their link to your account.