Skip to content

Instantly share code, notes, and snippets.

@Struka9
Created August 13, 2019 03:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Struka9/eabfec9e5b6c74c75db78bde7e2fb763 to your computer and use it in GitHub Desktop.
Save Struka9/eabfec9e5b6c74c75db78bde7e2fb763 to your computer and use it in GitHub Desktop.
// MyStartedService.kt
class MyStartedService: Service() {
private fun doWork(startId: Int, url: String) {
// Let's fetch something using the url
stopSelf(startId)
}
override fun (intent: Intent?, flags: Int, startId: Int): Int {
intent?.getStringExtra("EXTRA_URL")?.let {
doWork(startId, it)
}
return START_REDELIVER_INTENT
}
override fun onBind(intent: Intent): IBinder? {
// If we don't need to support binding for the service we return a null IBinder
return null
}
}
// MyActivity.kt
class MyActivity: AppCompatActivity() {
// ...
lateinit var fetchBt: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.my_activity)
// ... Complete the activity setup
fetchBt.setOnClickListener {
val intent = Intent(this@MyActivity, MyStartedService::class.java).apply {
putExtra("EXTRA_URL", myUrl)
}
startService(intent)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment