Skip to content

Instantly share code, notes, and snippets.

@nosix
Created July 9, 2016 12:22
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 nosix/19d1b58a20d65b1dd60216c012afdf9a to your computer and use it in GitHub Desktop.
Save nosix/19d1b58a20d65b1dd60216c012afdf9a to your computer and use it in GitHub Desktop.
UpdateReceiver for Android (SDK 22) in Kotlin 1.0.2
class MainActivity : AppCompatActivity() {
private val updateReceiver = UpdateReceiver(this)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
registerReceiver(updateReceiver, IntentFilter(SampleService.ACTION_SAMPLE))
}
class UpdateReceiver(val activity: MainActivity) : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
Log.d(TAG, intent.getStringExtra(SampleService.EXTRA_STRING))
}
}
}
class SampleService : IntentService(SampleService::class.simpleName) {
companion object {
val ACTION_SAMPLE = "ACTION_SAMPLE"
val EXTRA_STRING = "EXTRA_STRING"
}
override fun onHandleIntent(intent: Intent) {
sendBroadcast(Intent(ACTION_SAMPLE).putExtra(EXTRA_STRING, "sample"))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment