My Sms Service
My Sms Service
package leosib.raad.Sms;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.SmsManager;
/**
* Created by Andreas Kaltenleitner on 24.08.2016.
*/
public class MySmsService {
private static final String CODE_SENT = "SMS_SENT";
private static final String CODE_DELIVERED = "SMS_DELIVERED";
context.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
if (smsServiceListener != null)
smsServiceListener.OnSmsDelivered(sms, getResultCode());
}
}, new IntentFilter(CODE_DELIVERED));
This code defines a class called `MySmsService` that provides functionality for sending SMS messages in
an Android application. Let's go through the code and understand each part:
1. The code starts with the package declaration `package leosib.raad.Sms;`, indicating that this class
belongs to the `leosib.raad.Sms` package.
3. The class `MySmsService` is declared and it doesn't extend any other class.
4. Inside the class, two private constant strings are defined: `CODE_SENT` and `CODE_DELIVERED`. These
strings are used as identifiers for the broadcast intents that will be sent when an SMS is sent and
delivered.
5. The class has three member variables: `context`, `smsServiceListener`, and `sentPI`. `context` is an
instance of the `Context` class and is used to access the application context. `smsServiceListener` is an
instance of the `SmsServiceListener` interface, which allows a listener to be set for SMS events. `sentPI`
is a `PendingIntent` that will be used as a callback when the SMS is sent.
6. The constructor `MySmsService(Context context)` takes a `Context` object as a parameter and assigns
it to the `context` variable.
7. The method `setSmsServiceListener(SmsServiceListener smsServiceListener)` allows a listener of type
`SmsServiceListener` to be set for SMS events. This listener will be notified when an SMS is sent and
delivered.
8. The `sendSMS` method is defined, which takes a `MyMessage` object as a parameter. Inside this
method, two `PendingIntent`s are created: `sentPI` and `deliveredPI`. These `PendingIntent`s will be
used as callbacks when the SMS is sent and delivered, respectively.
9. Two `BroadcastReceiver`s are registered using the `context.registerReceiver` method. These receivers
listen for the broadcast intents with the actions specified by `CODE_SENT` and `CODE_DELIVERED`.
When these intents are received, the corresponding `onReceive` methods of the `BroadcastReceiver`s
will be called.
10. Inside the `onReceive` method of the first `BroadcastReceiver`, if the `smsServiceListener` is not null,
the `OnSmsSent` method of the listener is called with the `sms` object and the result code obtained
from `getResultCode()`.
11. Inside the `onReceive` method of the second `BroadcastReceiver`, if the `smsServiceListener` is not
null, the `OnSmsDelivered` method of the listener is called with the `sms` object and the result code
obtained from `getResultCode()`.
In summary, this code provides a service for sending SMS messages in an Android application. It allows
the registration of a listener to be notified when the SMS is sent and delivered, and it utilizes
`PendingIntent`s and `BroadcastReceiver`s to handle the callbacks and events related to SMS sending
and delivery.