Pro Android with Kotlin



Yüklə 4,6 Kb.
Pdf görüntüsü
səhifə52/231
tarix29.11.2023
ölçüsü4,6 Kb.
#138831
1   ...   48   49   50   51   52   53   54   55   ...   231
@de android telegram Pro Android with Kotlin Developing Modern Mobile

45
CHAPTER 5: Broadcasts
Thread.sleep(3000)
// or real work of course...
Log.e("LOG", "Broadcast done")
}
}
For local broadcasts, the receiver 
must
be declared inside the code. To avoid a resource 
leakage, we create and register the receiver inside 
onCreate()
and unregister it inside 
onDestroy()
.
class MainActivity : AppCompatActivity() {
private var bcReceiver:BroadcastReceiver? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// ...
bcReceiver = MyReceiver()
val ifi:IntentFilter =
IntentFilter("de.pspaeth.myapp.DO_STH")
LocalBroadcastManager.getInstance(this).
registerReceiver(bcReceiver, ifi)
}
override fun onDestroy() {
super.onDestroy()
// ...
LocalBroadcastManager.getInstance(this).
unregisterReceiver(bcReceiver)
}
}
Explicit Remote Broadcasts
We already pointed out that we can send broadcast messages of the 
remote
type to other 
apps or to the same app where the receivers live. The difference is in how the data is sent. 
For remote messages, the data goes through an IPC channel. Now to send such remote 
broadcast messages to the same app, you write the following:
val intent = Intent(this, MyReceiver::class.java)
intent.action = "de.pspaeth.myapp.DO_STH"
intent.putExtra("myExtra", "myExtraVal")
sendBroadcast(intent)


46
CHAPTER 5: Broadcasts
On the receiving side, for remote messages, the receiver 
must
be declared inside the 
manifest file.

...


"de.pspaeth.myapp.DO_STH">




Looking at the differences between local and remote broadcasts, it is helpful to keep the 
following in mind:
 

Yüklə 4,6 Kb.

Dostları ilə paylaş:
1   ...   48   49   50   51   52   53   54   55   ...   231




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə