Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
I'm grateful when for one drop in glass. Since I knw exactly what to do with it

Devrath devrath

💭
I'm grateful when for one drop in glass. Since I knw exactly what to do with it
View GitHub Profile
View CustomLocalBroadcastReceiver.kt
const val CUSTOM_ACTION = "custom-action-local-broadcast"
class CustomLocalBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent) {
val data = intent.getStringExtra("message")
Toast.makeText(context, data, Toast.LENGTH_LONG).show()
}
}
@devrath
devrath / ApplicationReceiverActivity.kt
Created Oct 5, 2022
Receiver for app to app communication
View ApplicationReceiverActivity.kt
package com.droid.code.demos.application_receiver
import android.content.Intent
import android.content.IntentFilter
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.droid.code.databinding.ActivityDynamicRecieverBinding
import com.droid.code.demos.application_receiver.reciever.ApplicationReceiverBroadcastReceiver
import com.droid.code.demos.dynamic_receiver.reciever.CustomDynamicBroadcastReceiver
@devrath
devrath / CustomDynamicBroadcastReceiver.kt
Created Oct 5, 2022
Registering and un-registering in dynamic recievers
View CustomDynamicBroadcastReceiver.kt
package com.droid.code.demos.dynamic_receiver.reciever
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.net.ConnectivityManager
import android.widget.Toast
const val KEY_AIRPLANE_MODE = "state"
@devrath
devrath / CustomStaticBroadcastReceiver.kt
Created Oct 4, 2022
Deprecated static broadcast reciever
View CustomStaticBroadcastReceiver.kt
class CustomStaticBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent) {
if (ConnectivityManager.CONNECTIVITY_ACTION == intent.action) {
Toast.makeText(context, "Connectivity changed", Toast.LENGTH_SHORT).show()
}
}
}
View UserOverriding.kt
sealed class User {
object Name : User(){
// We can override equals function
override fun equals(other: Any?): Boolean { return super.equals(other) }
// We can override hashCode function
override fun hashCode(): Int { return super.hashCode() }
// We can override toString function
override fun toString(): String { return super.toString() }
}
data object Age : User() {
@devrath
devrath / Age.kt
Created Sep 30, 2022
Data object class is same as normal object class
View Age.kt
data object Age {
private const val default : Int = 0
fun printDefaultAge(): Int {
return default
}
}
@devrath
devrath / MainActivity.kt
Created Sep 30, 2022
Sealed class implementation
View MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
println(User.Name)
println(User.Age)
}
}
View KotlinLanguageVersion.gradle
kotlinOptions {
jvmTarget = '1.8'
languageVersion = '1.8'
}
View settings.gradle
'org.jetbrains.kotlin.android' version '1.7.20'
@devrath
devrath / CustomBackgroundThread.kt
Created Sep 19, 2022
demo describing using handler in android to process runnables
View CustomBackgroundThread.kt
class CustomBackgroundThread : Thread() {
private var handler: Handler? = null
override fun run() {
super.run()
Looper.prepare() // Prepare the looper and associate with current thread
handler = Handler()
Looper.loop() // Start the thread so that it can keep the thread active
}