Created
June 12, 2025 14:34
-
-
Save Seniru/c991d97b479c8ebbc6e309c39143cb0f to your computer and use it in GitHub Desktop.
Services example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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