Implementing SMS, Email, and Call Functions in Android: A Step-by-Step Guide

Implementing SMS, Email, and Call Functions in Android: A Step-by-Step Guide

Sending an SMS Message in Android

SMS Manager is a class in Android that is used to send SMS to a specific contact from the Android application.

But firstly we need to get the user's permission to access SMS Management in Android, which the Manifest file takes care of.

<!-- AndroidManifest.xml -->
<uses-feature
        android:name="android.hardware.telephony"
        android:required="false" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<!-- The user telephony false attribute states that there is no 
need of the telephony directory of the device -->

Below is the implementation of a function on how an object of the SMS Manager class can be created to send an SMS Message. This function can be implemented & called whenever an SMS needs to be sent.

//Recipient Number & userMessage variable 
//store the phone number & message

private fun sendMessage(userMessage : String, recipientNumber : String){
  if(ContextCompat.
        checkSelfPermission(this,android.Manifest.permission.SEND_SMS) 
        != PackageManager.PERMISSION_GRANTED){
//We Check Whether the Permission defined by the Manifest in 
//denied by the user, hence we request the permission again
   ActivityCompat.requestPermissions(this, 
                    arrayOf(android.Manifest.permission.SEND_SMS),1)
  }
  else{
//runs if the permission was granted by the user initially.. 
      val smsManager =  this.getSystemService(SmsManager::class.java)
      smsManager.sendTextMessage(recipientNumber,null,
                                 userMessage,null,null)
  }
}

//Hence when the user responds to the Permission Request executed by the 
//if statement then we can override the onRequestPermissionResult method

override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
 super.onRequestPermissionsResult(requestCode, permissions, grantResults)
 if(requestCode == 1 && grantResults.isNotEmpty() && 
    grantResults[0]==PackageManager.PERMISSION_GRANTED){
        val smsManager =  this.getSystemService(SmsManager::class.java)
        smsManager.sendTextMessage(recipientNumber,null,
                                 userMessage,null,null)
 }
}

Sending Email in Android

Email can be sent in Android by defining the action using the Intent class. Hence, an Intent-Filter is used to define the action in the Manifest file :

<activity
   android:name=".MainActivity"
   android:exported="true">
   <intent-filter> ..  </intent-filter>
   <intent-filter>
<!--defines the send to action of the intent-->
       <action android:name="android.intent.action.SENDTO" />
<!--defines the data scheme as the data required in writing mails-->
       <data android:scheme="mailto"/>
       <category android:name="android.intent.category.DEFAULT" />
   </intent-filter>
 </activity>

Below is the implementation of a function on how an Intent object can be used to send an Email in Android. This function can be implemented & called whenever an Email needs to be sent.

fun sendEmail(address : String, subject : String, msg : String){
//takes the address, subject & message as parameter
  val emailAddresses = arrayOf(address) 
//as there can be multiple recipients
  val emailIntent = Intent(Intent.ACTION_SENDTO)
//creating a intent object
  emailIntent.data = Uri.parse("mailto:")
//defining the data scheme to be of the mail type 
  emailIntent.putExtra(Intent.EXTRA_EMAIL, emailAddresses) 
//defining the data scheme type to be of email-address & passing address
  emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject) 
//defining the data scheme type to be of email-subject & passing subject
  emailIntent.putExtra(Intent.EXTRA_TEXT, msg)
//defining the data scheme type to be of email-message & passing msg

  if(emailIntent.resolveActivity(packageManager) != null) { 
//checking if there is any application capable of sending email 
     startActivity(Intent.createChooser(emailIntent,"Choose App: "))
//provide a chooser to perform the email for the emailIntent
  } 
}

Making a Call in Android

A call can be made in Android by defining the call action using the Intent class. However, we need to get the user permission from the Manifest file.

<!--AndroidManifest.xmk-->
<uses-permission android:name="android.permission.CALL_PHONE"
        tools:ignore="PermissionImpliesUnsupportedChromeOsHardware" />

Below is the implementation of a function on how an Intent object can be used to make a call in Android. This function can be implemented & called whenever a call needs to be made.

fun startCall(phoneNumber : String){
  if(ContextCompat.
        checkSelfPermission(this,android.Manifest.permission.CALL_PHONE) 
        != PackageManager.PERMISSION_GRANTED){
//We Check Whether the Permission defined by the Manifest in 
//denied by the user, hence we request the permission again
   ActivityCompat.requestPermissions(this, 
                    arrayOf(android.Manifest.permission.CALL_PHONE),1)
  }
  else{ 
      val intent = Intent(Intent.ACTION_CALL)
      intent.data = Uri.parse("tel:$phoneNumber")
      startActivity(intent)
  }
}

//Hence when the user responds to the Permission Request executed by the 
//if statement then we can override the onRequestPermissionResult method

override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
 super.onRequestPermissionsResult(requestCode, permissions, grantResults)
 if(requestCode == 1 && grantResults.isNotEmpty() && 
    grantResults[0]==PackageManager.PERMISSION_GRANTED){
        val intent = Intent(Intent.ACTION_CALL)
        intent.data = Uri.parse("tel:$phoneNumber")
        startActivity(intent)
 }
}