Ir al contenido principal

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.

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.

on-Activity.java

Comentarios

Entradas populares de este blog

What is PDU on Android?

A PDU is a “protocol data unit”, which is the industry format for an SMS message. because SMSMessage reads/writes them you shouldn’t need to disect them. A large message might be broken into many, which is why it is an array of objects.   PDU, or protocol data unit, refers to one method of sending information along cellular networks. SMS messaging, as described in the 3rd Generation Partnership Project  (3GPP) Specification, supports two different ways of sending and receiving messages. The first is text mode, which some phones don’t support. Text mode encodes message content as a simple bit stream. The other is PDU mode, which contains not  only the SMS message, but also metadata about the SMS message, such as text encoding, the sender, SMS service center address, and much more. To access this metadata, mobile SMS applications almost always use PDUs to encode the contents of a SMS message.