0% found this document useful (0 votes)
2 views1 page

LabPreppp 6

The document contains Kotlin code for an Android application with a MainActivity and a MyReceiver class. The MainActivity registers a BroadcastReceiver to listen for changes in airplane mode and Bluetooth state, updating a TextView accordingly. The MyReceiver class updates the TextView to display 'ON' or 'OFF' based on the state of airplane mode.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

LabPreppp 6

The document contains Kotlin code for an Android application with a MainActivity and a MyReceiver class. The MainActivity registers a BroadcastReceiver to listen for changes in airplane mode and Bluetooth state, updating a TextView accordingly. The MyReceiver class updates the TextView to display 'ON' or 'OFF' based on the state of airplane mode.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

MainActivity.

kt
class MainActivity : AppCompatActivity() {

@SuppressLint("MissingInflatedId", "Range")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val tv: TextView = findViewById(R.id.textViewStatus)

val r = MyReceiver(tv)
val filter = IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED)
registerReceiver(r, filter)

override fun onDestroy() {


super.onDestroy()

}
}

MyReceiver.kt
class MyReceiver(private val textView: TextView) : BroadcastReceiver() {

override fun onReceive(context: Context, intent: Intent) {


val isON = Settings.Global.getInt(
context.contentResolver,
Settings.Global.AIRPLANE_MODE_ON, // Settings.Global.BLUETOOTH_ON
0
)
textView.text = if(isON != 0){"ON"} else{"OFF"}

}
}

MainActivity.kt
class MainActivity : AppCompatActivity() {

@SuppressLint("MissingInflatedId", "Range")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val tv: TextView = findViewById(R.id.textViewStatus)

val r = MyReceiver(tv)
val filter = IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED)
registerReceiver(r, filter)

override fun onDestroy() {


super.onDestroy()

}
}

You might also like