Using a Service to Launch notification when receiving an SMS from the BroadcastReciever when the application has been closed.
In programming there are multiple ways to solve a problem or a particular question.Only each one of us selects either the simplest or the most complicated, or simply the first one that comes to mind.So that solution to the problem is not perhaps the best but it is one of them and it is a very practical one.
For more information click here, Services
A broadcast receiver is implemented as a subclass of BroadcastReceiver and each broadcast is delivered as an Intent object. For more information, see the BroadcastReceiver class.
More on the Documentation, Application Fundamentals, Common Tasks :
Well, in my process I have the service start when I close my application. In this way the service will be running while the application is not active. In the broadcastReciever I check if the service is running and if this is true, then I send the notification.
At the end of the smsReciever.java file is the method that creates the notification.
Before I start explaining step by step what I did to get to the end of the solution it is important to talk about some concepts, and some classes that are important to reference and define conceptually.
1. What is a Service on Android?
A Service is an application component that can perform long-running operations in the background and does not provide a user interface. Another application component can start a service and it will continue to run in the background even if the user switches to another application. Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC). For example, a service might handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background.For more information click here, Services
MyService.java
1. What is a BoadcastReciever?
A broadcast receiver is a component that responds to system-wide broadcast announcements. Many broadcasts originate from the system—for example, a broadcast announcing that the screen has turned off, the battery is low, or a picture was captured. Applications can also initiate broadcasts—for example, to let other applications know that some data has been downloaded to the device and is available for them to use. Although broadcast receivers don't display a user interface, they may create a status bar notification to alert the user when a broadcast event occurs. More commonly, though, a broadcast receiver is just a "gateway" to other components and is intended to do a very minimal amount of work. For instance, it might initiate a service to perform some work based on the event.A broadcast receiver is implemented as a subclass of BroadcastReceiver and each broadcast is delivered as an Intent object. For more information, see the BroadcastReceiver class.
More on the Documentation, Application Fundamentals, Common Tasks :
smsReciever.java
Well, in my process I have the service start when I close my application. In this way the service will be running while the application is not active. In the broadcastReciever I check if the service is running and if this is true, then I send the notification.
At the end of the smsReciever.java file is the method that creates the notification.
Comentarios
Publicar un comentario