[go: up one dir, main page]

0% found this document useful (0 votes)
29 views3 pages

My Sms Service

This code defines a class called MySmsService that provides functionality for sending SMS messages from an Android application. It allows a listener to be set to receive callbacks when an SMS is sent and delivered. When sendSMS is called, it uses PendingIntents and BroadcastReceivers to send the SMS via SmsManager and notify the listener of the send and delivery status.

Uploaded by

Muhammad Asad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views3 pages

My Sms Service

This code defines a class called MySmsService that provides functionality for sending SMS messages from an Android application. It allows a listener to be set to receive callbacks when an SMS is sent and delivered. When sendSMS is called, it uses PendingIntents and BroadcastReceivers to send the SMS via SmsManager and notify the listener of the send and delivery status.

Uploaded by

Muhammad Asad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Code:

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";

private Context context;


private SmsServiceListener smsServiceListener;

public MySmsService(Context context) {


this.context = context;
}

public void setSmsServiceListener(SmsServiceListener smsServiceListener) {


this.smsServiceListener = smsServiceListener;
}

public void sendSMS(final MyMessage sms) {


PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, new
Intent(CODE_SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(context, 0,
new Intent(CODE_DELIVERED), 0);

//TODO set timeout


context.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
if (smsServiceListener != null)
smsServiceListener.OnSmsSent(sms, getResultCode());
}
}, new IntentFilter(CODE_SENT));

context.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
if (smsServiceListener != null)
smsServiceListener.OnSmsDelivered(sms, getResultCode());
}
}, new IntentFilter(CODE_DELIVERED));

SmsManager smsManager = SmsManager.getDefault();


smsManager.sendTextMessage(sms.getPhoneNumber(), null,
sms.getMessage(), sentPI,
deliveredPI);
}
}

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.

2. The necessary imports are included, such as `android.app.PendingIntent`,


`android.content.BroadcastReceiver`, `android.content.Context`, `android.content.Intent`, and
`android.telephony.SmsManager`. These imports allow the code to use classes and methods from the
Android SDK.

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()`.

12. Finally, an instance of `SmsManager` is obtained using `SmsManager.getDefault()`, and the


`sendTextMessage` method is called to send the SMS message. The message content, recipient phone
number, and the `PendingIntent`s for sent and delivered events are provided as parameters to this
method.

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.

You might also like