Pro Android with Kotlin
Download 5.42 Mb. Pdf ko'rish
|
@de android telegram Pro Android with Kotlin Developing Modern Mobile
- Bu sahifa navigatsiya:
- Table 5-3.
51
CHAPTER 5: Broadcasts Table 5-3 shows some examples (one filter only; if there are several filters, the matching happens on an “or” basis). Active or On-Hold Listening Which state must an app be in to be able to receive implicit broadcasts? If we want a broadcast receiver to be just registered in the system and fired up only on demand when a matching broadcast arrives, the listener must be specified in the manifest file of the app. However, for implicit broadcasts, this cannot be freely done. It is only for predefined system broadcasts, as listed in section “System Broadcasts” of the online text companion. Table 5-3. Data Matches Receiver Type Receiver URI .* = any string Sender Match text/html type = "text/html" Yes text/html text/plain type = "text/html" Yes text/html text/plain type = "image/jpeg" No http://a.b.c/xyz data = "http://a.b.c/xyz" Yes http://a.b.c/xyz data = "http://a.b.c/qrs" No http://a.b.c/xyz/.* data = "http://a.b.c/xyz/3" Yes http://.*/xyz data = "http://a.b.c/xyz" Yes http://.*/xyz data = "http://a.b.c/qrs" No text/html http://a.b.c/xyz/.* type = "text/html" data = "http://a.b.c/xyz/1" Yes text/html http://a.b.c/xyz/.* type = "image/jpeg" data = "http://a.b.c/xyz/1" No Note This restriction for implicit intent filters specified in the manifest was introduced in Android 8.0 (API level 26). Before that, any implicit filters could be specified inside the manifest file. 52 CHAPTER 5: Broadcasts If, however, you start your broadcast listeners programmatically from inside an app and this app is running, you can define as many implicit broadcast listeners as you want, and there is no restriction on whether the broadcasts come from the system, your app, or other apps. Likewise, there is no restriction on usable action or category names. Since listening for booting-completed events is included in the list for allowed listeners inside the manifest file, you are free to start apps there as activities or services, and inside those apps you can register any implicit listener. But that means you can legally work around the restrictions imposed starting with Android 8.0. Just be aware that such apps may be killed by the Android OS if resource shortages occur, so you have to take appropriate precautions. Sending Implicit Broadcasts To prepare to send an implicit broadcast, you specify actions, categories, data, and extra data as follows: val intent = Intent() intent.action = "de.pspaeth.myapp.DO_STH" intent.addCategory("de.pspaeth.myapp.CATEG1") intent.addCategory("de.pspaeth.myapp.CATEG2") // ... more categories intent.type = "text/html" intent.data = Uri.parse("content://myContent") intent.putExtra("EXTRA_KEY", "extraVal") intent.flags = ... Only the action is mandatory; all the others are optional. Now to send the broadcast, you’d write the following for a remote broadcast: sendBroadcast(intent) For a local broadcast, you’d write this: LocalBroadcastManager.getInstance(this). sendBroadcast(intent) The this must be a Context or a subclass thereof; it will work exactly like shown here if the code is from inside an activity or a service class. For remote messages, there is also a variant sending a broadcast to applicable receivers one at a time. ... sendOrderedBroadcast(...) ... This makes the receivers get the message sequentially, and each receiver may cancel, forwarding the message to the next receiver in the line by using BroadcastReceiver. abortBroadcast(). |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling