Skip to content

Instantly share code, notes, and snippets.

@Seniru
Created June 12, 2025 14:34
Show Gist options
  • Select an option

  • Save Seniru/c991d97b479c8ebbc6e309c39143cb0f to your computer and use it in GitHub Desktop.

Select an option

Save Seniru/c991d97b479c8ebbc6e309c39143cb0f to your computer and use it in GitHub Desktop.
Services example
package com.seniru.servicesexample
import android.app.Service
import android.content.Intent
import android.os.IBinder
import android.util.Log
class CountingService : Service() {
private var isRunning = false
var count = 0
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if (!isRunning) {
isRunning = true
Thread {
while (isRunning) {
count++
Log.d("CountingService", "Count: $count")
Thread.sleep(1000)
}
}.start()
}
return START_STICKY
}
override fun onBind(intent: Intent?): IBinder? = null
}
package com.seniru.servicesexample
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.IBinder
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val intent = Intent(this, CountingService::class.java)
startService(intent)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment