BCA / B.Tech 11 min read

Communication Services: SMS and Email

Communication Services

1: SMS Messaging

Android applications can send SMS messages programmatically. This is done using the `SmsManager` class.

Step 1: Add the Permission

To send an SMS, your app requires the `SEND_SMS` permission in the `AndroidManifest.xml` file.


<manifest ...>
    <uses-permission android:name="android.permission.SEND_SMS" />
    <application ...>
        ...
    </application>
</manifest>

Note: For Android 6.0 (API 23) and above, you must also request the permission from the user at runtime.

Step 2: Sending the SMS in Java

You can get an instance of the `SmsManager` class and use the `sendTextMessage()` method.

Example in Java:


public class SmsActivity extends AppCompatActivity {
    EditText phoneEditText, messageEditText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sms);

        phoneEditText = findViewById(R.id.phone_et);
        messageEditText = findViewById(R.id.message_et);

        // Runtime permission check (For API 23+)
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, 1);
        }
    }

    public void sendSms(View view) {
        String phoneNumber = phoneEditText.getText().toString();
        String message = messageEditText.getText().toString();

        if (!phoneNumber.isEmpty() && !message.isEmpty()) {
            try {
                SmsManager smsManager = SmsManager.getDefault();
                smsManager.sendTextMessage(phoneNumber, null, message, null, null);
                Toast.makeText(getApplicationContext(), "SMS sent.", Toast.LENGTH_LONG).show();
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "SMS failed to send.", Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }
        } else {
            Toast.makeText(this, "Please enter phone number and message", Toast.LENGTH_SHORT).show();
        }
    }
}

2: Sending Email

Android does not have a built-in API for sending emails directly. Instead, Android uses an Implicit Intent to launch an email client (like Gmail, Outlook) that is already installed on the device to handle the email sending.

This approach is secure and user-friendly because the user gets to use their trusted email app.

Steps to Send an Email:

  1. Create a new `Intent` object with the action `Intent.ACTION_SEND`.
  2. Add email details (like recipient, subject, and body) using the `putExtra()` method.
  3. Set the intent's type using `setType("message/rfc822")` to ensure only email clients can handle it.
  4. Launch the intent using `startActivity()`.

Example of an Email Intent in Java:


public class EmailActivity extends AppCompatActivity {
    EditText recipientEditText, subjectEditText, bodyEditText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_email);

        recipientEditText = findViewById(R.id.recipient_et);
        subjectEditText = findViewById(R.id.subject_et);
        bodyEditText = findViewById(R.id.body_et);
    }

    public void sendEmail(View view) {
        String recipient = recipientEditText.getText().toString();
        String subject = subjectEditText.getText().toString();
        String body = bodyEditText.getText().toString();

        Intent emailIntent = new Intent(Intent.ACTION_SEND);
        
        // Set the data
        emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{recipient}); // Recipient(s)
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject); // Subject
        emailIntent.putExtra(Intent.EXTRA_TEXT, body); // Body

        // Set the type
        emailIntent.setType("message/rfc822");

        // Start the activity, giving the user a choice of email clients
        startActivity(Intent.createChooser(emailIntent, "Choose an email client:"));
    }
}