Pro Android with Kotlin


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

53
CHAPTER 5: Broadcasts
Receiving Implicit Broadcasts
To receive an implicit broadcast, for a limited set of broadcast types (see the section “The 
System Intent Filters” of the online text companion) you can specify a BroadcastListener 
inside AndroidManifest.xml as follows:

...


"com.xyz.myapp.DO_STH" />
"android.intent.category.DEFAULT"/>
"com.xyz.myapp.MY_CATEG"/>
android:port="80"
android:host="com.xyz"
android:path="items/7"
android:mimeType="text/html" />



The  element shown here is just an example; see Chapter 
3
 for all the possibilities.
In contrast to that, adding a programmatic listener for implicit broadcasts to your code is 
unrestricted.
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")
registerReceiver(bcReceiver, ifi)
}
override fun onDestroy() {
super.onDestroy()
// ...
unregisterReceiver(bcReceiver)
}
}
The MyReceiver is an implementation of class android.content.BroadcastReceiver.


54
CHAPTER 5: Broadcasts
Listening to System Broadcasts
To listen to system broadcasts, see the list in the online text companion’s “System 
Broadcasts” section. You can just use a programmatic registration as shown earlier. For 
most of them, you cannot use the manifest registration method for background execution 
limits imposed since Android 8.0 (API level 26). However, for a number of them, you can also 
use the manifest file to specify listeners.

ACTION_LOCKED_BOOT_COMPLETED, ACTION_BOOT_COMPLETED:
Apps may need those to schedule jobs, alarms, and so on.

ACTION_USER_INITIALIZE, "android.intent.action.USER_ADDED", 
"android.intent.action.USER_REMOVED":
These are protected by privileged permissions, so the use cases are 
limited.

"android.intent.action.TIME_SET", ACTION_TIMEZONE_CHANGED, ACTION_
NEXT_ALARM_CLOCK_CHANGED:
These are needed by clock apps.

ACTION_LOCALE_CHANGED:
The locale changed, and apps might need to update their data when this 
happens.

ACTION_USB_ACCESSORY_ATTACHED, ACTION_USB_ACCESSORY_DETACHED, 
ACTION_USB_DEVICE_ATTACHED, ACTION_USB_DEVICE_DETACHED:
These are USB-related events.

ACTION_CONNECTION_STATE_CHANGED, ACTION_ACL_ CONNECTED,
ACTION_ACL_DISCONNECTED:
These are Bluetooth events.

ACTION_CARRIER_CONFIG_CHANGED, TelephonyIntents.
ACTION_*_SUBSCRIPTION_CHANGED,"TelephonyIntents.
SECRET_CODE_ACTION":
OEM telephony apps may need to receive these broadcasts.

LOGIN_ACCOUNTS_CHANGED_ACTION:
This is needed by some apps to set up scheduled operations for new 
and changed accounts.

ACTION_PACKAGE_DATA_CLEARED:
Data is cleared by the OS Settings app; a running app likely is interested
in that.

ACTION_PACKAGE_FULLY_REMOVED:
Related apps might need to be informed if some apps get uninstalled
and their data is removed.



Download 5.42 Mb.

Do'stlaringiz bilan baham:
1   ...   52   53   54   55   56   57   58   59   ...   223




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