Introducci贸n a la Integraci贸n Nativa en Android
Integra APIs Nativas en Android
Creaci贸n proyecto base
Google Maps SDK
C贸mo crear una API key para Google Maps en Google Cloud
Creaci贸n de marcadores en Google Maps con Jetpack Compose
C贸mo dibujar rutas en mapas usando polil铆neas en Jetpack Compose
C贸mo enfocar autom谩ticamente mapas usando Camera Update Factory
Quiz: Google Maps SDK
Servicios de Localizaci贸n
Uso de Flows en Kotlin para Controlar Intervalos de Tiempo y Emisi贸n de Datos
C贸mo simular ubicaci贸n en emuladores y dispositivos reales
Creaci贸n de Modelos y C谩lculos de Localizaci贸n con Clean Architecture
Implementaci贸n de Localizaci贸n en Android Usando Flows
Inyecci贸n de dependencia para seguimiento de localizaci贸n en Android
Uso de StateFlows para rastrear ubicaci贸n en aplicaciones Android
Location Tracker
Implementaci贸n de Location Tracker con Inyecci贸n de Dependencias
Quiz: Servicios de Localizaci贸n
Integraci贸n Maps con Localizaci贸n
Integraci贸n de mapas din谩micos con CameraPositionState en Android
Creaci贸n y uso de polil铆neas en mapas con datos reales
Creaci贸n de una pantalla de mapa con Intents y estados en Jetpack Compose
Creaci贸n de un ViewModel para Seguimiento de Localizaci贸n en Android
Quiz: Integraci贸n Maps con Localizaci贸n
Manejo de permisos
Gesti贸n de permisos en Android para localizaci贸n, c谩mara y notificaciones
C贸mo implementar di谩logos para solicitar permisos en Android
Manejo de permisos de localizaci贸n y notificaci贸n en Android
C贸mo gestionar permisos en Android con Jetpack Compose
Quiz: Manejo de permisos
Integraci贸n c谩mara
Integraci贸n de c谩mara en Android con Photo Handler y manejo de permisos
Convierte Bitmaps a ByteArrays en Android con Kotlin
Creaci贸n de intents y estados UI para c谩mara en Android con Kotlin
Implementaci贸n de funciones clave en ViewModel para c谩mara Android
Integrar C谩maraX en Jetpack Compose para Android
Captura y previsualizaci贸n de fotos en Android con Jetpack Compose
C贸mo Mostrar Fotos en Marcadores de Ubicaci贸n en Mapas con Jetpack Compose
Quiz: Integraci贸n c谩mara
Servicios en Android
Implementaci贸n de servicios en Android: normal services y foreground services
Implementar Foreground Services en Android para Persistencia en Segundo Plano
Quiz: Servicios en Android
Transmisiones en Android (Broadcast)
Implementaci贸n de BroadcastReceiver en Android para Escuchar Eventos del Sistema
Pruebas finales y cierre
You don't have access to this class
Keep learning! Join and start boosting your career
Understanding how Broadcast Receivers work in Android is key to developing responsive and efficient applications. These receivers allow you to listen and respond to specific operating system or custom events, such as connecting a USB or activating airplane mode.
Broadcast Receivers are components designed to be attentive to events broadcast by Android. Some common examples are:
This component is not limited only to system events, you can also create and listen to events specifically defined in the application.
Creating a Broadcast Receiver involves defining a class that directly inherits from the BroadcastReceiver provided by Android and overwriting its onReceive
method to catch the events broadcast:
class StopReceiver : BroadcastReceiver() { private lateinit var photoHandler: PhotoHandler private lateinit var locationTracker: LocationTracker private val applicationScope = CoroutineScope() companion object { const val ACTION_STOP = "TrackIt.Action.StopTracking" } override fun onReceive(context: Context?, intent: Intent?) { val action = intent?.action ?: return if (action != ACTION_STOP) return applicationScope.launch { locationTracker.setIsTrackingToFalse() locationTracker.stopObservingLocation() photoHandler.clearPhotos().join() val serviceIntent = Intent(context, TrackingService::class.java).apply { this.action = TrackingService.ACTION_STOP } if (TrackingService.isActive.value) { context?.startService(serviceIntent) } } } }}
Subsequently, it is important to register the BroadcastReceiver in the AndroidManifest.xml
file by explicitly indicating that you do not want other applications to invoke this receiver:
<receiver android:name=".StopReceiver" android:exported="false" />
To interact with the Broadcast Receiver, especially from a notification when the application is in the background or closed, you need to assign a PendingIntent
that triggers the receiver:
val stopIntent = Intent(applicationContext, StopReceiver::class.java).apply { action = StopReceiver.ACTION_STOP}val stopPendingIntent = PendingIntent.getBroadcast( applicationContext, 0, stopIntent, PendingIntent.FLAG_IMMUTABLE)val notificationAction = NotificationCompat.Action( R.drawable.launcher_foreground, applicationContext.getString(R.string.stop), stopPendingIntent)// Finally, this action is added to the notification.notificationBuilder.addAction(notificationAction)
With this configuration, when you press the "Stop" button in the notification, the Broadcast Receiver will catch the action and execute the tasks you have defined, such as stopping services or freeing resources.
Have you worked with Broadcast Receivers before, and what other practical applications can you think of for these receivers?
Contributions 0
Questions 0
Want to see more contributions, questions and answers from the community?