Skip to content

Instantly share code, notes, and snippets.

@robertlevonyan
Created June 22, 2020 12:14
Show Gist options
  • Save robertlevonyan/91aae6a3633cce9fd39335a88850a533 to your computer and use it in GitHub Desktop.
Save robertlevonyan/91aae6a3633cce9fd39335a88850a533 to your computer and use it in GitHub Desktop.
class HeadsetConnectionReceiver(private val actionPlugged: (Boolean) -> Unit) : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
intent?.run {
if (Intent.ACTION_HEADSET_PLUG == action) {
when (getIntExtra("state", -1)) {
0 -> actionPlugged(false)
1 -> actionPlugged(true)
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment