Pro Android with Kotlin


Download 5.42 Mb.
Pdf ko'rish
bet58/223
Sana04.11.2023
Hajmi5.42 Mb.
#1746807
1   ...   54   55   56   57   58   59   60   61   ...   223
Bog'liq
@de android telegram Pro Android with Kotlin Developing Modern Mobile

57
CHAPTER 5: Broadcasts
If you use local broadcasts with the LocalBroadcastManager, you cannot specify permissions 
on the sender or the receiver side.
Securing Implicit Broadcasts
Like nonlocal explicit broadcasts, the permissions in implicit broadcasts can be specified on 
both the broadcast sender and the receiver side. On the sender side, you would write the 
following:
val intent = Intent()
intent.action = "de.pspaeth.myapp.DO_STH"
// ... more intent coordinates
sendBroadcast(intent, "com.xyz.theapp.PERMISSION1")
This expresses sending a broadcast to all matching receivers that are additionally protected 
by com.xyz.theapp.PERMISSION1. Of course, you should write your own package names 
here and use the appropriate permission names. As for the usual sender-receiver matching 
procedure for implicit broadcasts, adding a permission kind of serves as an additional 
matching criterion, so if there are several receiver candidates looking at the intent filters, for 
actually receiving this broadcast, only those will be picked out that additionally provide this 
permission flag.
One more thing that needs to be taken care of for implicit broadcasts is specifying the 
permission usage in AndroidManifest.xml. So, for this sender to be able to use the 
permission, add the following to the manifest file:

It’s the same as for explicit broadcasts. Sending a broadcast without a permission 
specification may address receivers with and without permission protection.
...
sendBroadcast(intent)
...
This means specifying permissions on the sender side is not supposed to tell the receiver 
that the sender is protected in any way.
For a receiver to be able to get hold of such a broadcast, the permission must be added to 
the code like this:
private var bcReceiver: BroadcastReceiver? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
...
bcReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?,
intent: Intent?) {



Download 5.42 Mb.

Do'stlaringiz bilan baham:
1   ...   54   55   56   57   58   59   60   61   ...   223




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling