Android Sending SMS

Android Sending SMS

Android Sending SMS This tutorial is all about that how you can send sms by using android SmsManager API.In Google android, you may use android SmsManager API or devices Built-in Text request to send SMS’s. This training explains to Mailing SMS Meaning In Google android using SMS intention method or by using using android SmsManager API. In this Tutorial, we teaches you two basic cases to send Text message concept using android SmsManagaer API

android sending sms

Android SmsManager API

SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("phoneNo", null, "sms message", null, null);

Built-in SMS application

Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", "default content"); 
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);

Of course, both need SEND_SMS permission.

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

 

From the above method aside, there are few other important functions available in SmsManager class. These procedures are the following –

Methods & Descriptions of Android Sending SMS

ArrayList<String> divideMessage(String text)

A note is divided by this technique content material into several fragments, none of them bigger than the utmost SMS meaning size.

static SmsManager getDefault()

This method is employed to obtain the default case of the SmsManager

void sendDataMessage(String destinationAddress, String scAddress, brief destinationPort, byte[] data, PendingIntent sentIntent, PendingIntent deliveryIntent)

This method can be used to send a data structured SMS to a particular application port.

void sendMultipartTextMessage(String destinationAddress, String scAddress, ArrayList<String> parts, ArrayList<PendingIntent> sentIntents, ArrayList<PendingIntent> deliveryIntents)

This method is use to Send a multi-part text message based SMS.

void sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)

This method is used to Send a text based SMS.

Android Sending SMS Example

Following android sending sms example teaches you in sensible how to use SmsManager subject to send an Text to the given phone number.

To test out this android sending sms example, you’ll need genuine Mobile device outfitted with latest Google android Operating-system, otherwise you will have to struggle with emulator which might not exactly work.

Steps    &      Descriptions of Android Sending SMS

1 –You use Android Studio room IDE to set-up an Android program and name it as androdevelopment under a offer com.example.androdevelopment. While creating this task, be sure to Goal SDK and Compile With at the latest version of Android os SDK to utilize higher degrees of APIs.

2 –Modify src/MainActivity.java and add required code to manage sending email.

3-Modify design XML res/design/activity_main.xml add any GUI part if required. I’m adding a straightforward GUI for taking mobile amount and SMS word to be delivered and a straightforward button to send Text message.

4-No need to identify default string constants at res/ideals/strings.xml. Google android studio manages default constants.

5 –Modify AndroidManifest.xml as shown below

6-Run the application form to launch Google android emulator and validate the consequence of the changes done in the application form.

Following is the content of the modified main activity file src/com.example.androdevelopment/MainActivity.java.

package com.example.androdevelopment;

import android.os.Bundle;
import android.app.Activity;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
   Button sendBtn;
   EditText txtphoneNo;
   EditText txtMessage;
   
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      sendBtn = (Button) findViewById(R.id.btnSendSMS);
      txtphoneNo = (EditText) findViewById(R.id.editText);
      txtMessage = (EditText) findViewById(R.id.editText2);

      sendBtn.setOnClickListener(new View.OnClickListener() {
         public void onClick(View view) {
            sendSMSMessage();
         }
      });
   }
   protected void sendSMSMessage() {
      Log.i("Send SMS", "");
      String phoneNo = txtphoneNo.getText().toString();
      String message = txtMessage.getText().toString();
      
      try {
         SmsManager smsManager = SmsManager.getDefault();
         smsManager.sendTextMessage(phoneNo, null, message, null, null);
         Toast.makeText(getApplicationContext(), "SMS sent.", Toast.LENGTH_LONG).show();
      } 
      
      catch (Exception e) {
         Toast.makeText(getApplicationContext(), "SMS faild, please try again.", Toast.LENGTH_LONG).show();
         e.printStackTrace();
      }
   }
   
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
   }
}

Following will be the content of res/layout/activity_main.xml file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context="MainActivity">

   <TextView
      android:id="@+id/textView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Sending SMS Example"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:textSize="30dp" />
      
   <TextView
      android:id="@+id/textView2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Andro Development "
      android:textColor="#ff87ff09"
      android:textSize="30dp"
      android:layout_below="@+id/textView1"
      android:layout_alignRight="@+id/imageButton"
      android:layout_alignEnd="@+id/imageButton" />
      
   <ImageButton
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imageButton"
      android:src="@drawable/abc"
      android:layout_below="@+id/textView2"
      android:layout_centerHorizontal="true" />
      
   <EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/editText"
      android:hint="Enter Mobile Number"
      android:phoneNumber="true"
      android:textColorHint="@color/abc_primary_text_material_dark"
      android:layout_below="@+id/imageButton"
      android:layout_centerHorizontal="true" />

   <EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/editText2"
      android:layout_below="@+id/editText"
      android:layout_alignLeft="@+id/editText"
      android:layout_alignStart="@+id/editText"
      android:textColorHint="@color/abc_primary_text_material_dark"
      android:layout_alignRight="@+id/imageButton"
      android:layout_alignEnd="@+id/imageButton"
      android:hint="Enter Your SMS" />

   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Send Sms"
      android:id="@+id/btnSendSMS"
      android:layout_below="@+id/editText2"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="48dp" />

</RelativeLayout>

 

Following will be the content of res/values/strings.xml to define two new constants

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="app_name">androdevelopment</string>
   <string name="action_settings">Settings</string>
</resources>

 

Following is the default content of AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.androdevelopment"
   android:versionCode="1"
   android:versionName="1.0" >
   
   <uses-sdk
      android:minSdkVersion="8"
      android:targetSdkVersion="22" />
   <uses-permission android:name="android.permission.SEND_SMS" />
   
   <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      
      <activity
         android:name="com.example.androdevelopment.MainActivity"
         android:label="@string/app_name" >
         
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
         
      </activity>
      
   </application>
</manifest>

 

Let’s make an effort to run your androdevelopment app(Android Sending SMS app). I suppose you have linked your actual Google android Mobile device with your personal computer. To perform the app from Android studio room, wide open one of your project’s activity data and click Run Eclipse Run Icon from the toolbar. Prior to starting your application, Android os studio room installer will screen following window to choose a choice where you want to perform your Android software.

android sending sms


Do You Have Any Question About This Tutorial? Ask Question Now On Andro Development & Get Answer Instantly. No Signup/Login Needed


You will get into a desired phone number and a text to be directed on that number. Select Send SMS message button to send your Text finally. Ensure that your GSM/CDMA connection is working fine to provide your SMS to its recipient.

You may take lots of SMS segregated by comma and then within your program you will need to parse them into a wide range string and lastly you may use a loop to send meaning to all or any the given amounts. That’s ways to write your own Text message client. Next section will highlight how to use existing Text customer to send Text.

Android Sending SMS By Another Method

Using Built-in Intent to send SMS

You should use Android Intention to send Text by dialling built-in SMS efficiency of the Google android. Pursuing section explains various areas of our Intent thing necessary to send an Text message.

Intent Object – Action to send SMS

You shall use ACTION_VIEW action to kick off an Text customer installed on your Android os device. Following is easy syntax to make an intent with ACTION_VIEW action

Intent smsIntent = new Intent(Intent.ACTION_VIEW);

 

Intent Object – Data/Type to send SMS

To send an Text you will need to identify smsto: as URI using setData() method and data type is to vnd.android-dir/mms-sms using setType() method the following

smsIntent.setData(Uri.parse("smsto:"));
smsIntent.setType("vnd.android-dir/mms-sms");

 

Intent Object – Extra to send SMS

Android os has built-in support to include contact number and text to send an Text message as follows

smsIntent.putExtra("address"  , new String("0123456789;3393993300"));
smsIntent.putExtra("sms_body"  , "Test SMS to Sappal");

 

Here sms_body and address are circumstance very sensitive and really should be given in small people only. You are able to specify several number in single string but separated by semi-colon (;).

Android Sending SMS Example

Following android sending sms example teaches you in useful how to use intent object to start SMS consumer to send an Text message to the given recipients.

To test out this android sending sms example, you’ll need genuine Mobile device prepared with latest Android os Operating-system, otherwise you shall have to struggle with emulator which may well not work.

Steps    &    Descriptions

1-You use Android studio room IDE to generate an Android software and name it as tutorialspoint under a program com.example.androdevelopment. While creating this task, ensure you Aim for SDK and Compile With at the latest version of Google android SDK to make use of higher level of Android APIs.

2-Modify src/MainActivity.java and add required code to manage sending SMS.

3-Modify design XML res/design/activity_main.xml add any GUI element if required. I’m adding a straightforward button to kick off SMS Client.

4-No need to identify default constants.Google android studio manages default constants.

5-Modify AndroidManifest.xml as shown below

6 –Run the application form to launch Google android emulator and validate the consequence of the changes done in the application form.

Following is the content of the modified main activity file src/com.example.androdevelopment/MainActivity.java.

package com.example.androdevelopment;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      Button startBtn = (Button) findViewById(R.id.button);
      startBtn.setOnClickListener(new View.OnClickListener() {
         public void onClick(View view) {
            sendSMS();
         }
      });
   }
   
   protected void sendSMS() {
      Log.i("Send SMS", "");
      Intent smsIntent = new Intent(Intent.ACTION_VIEW);
      
      smsIntent.setData(Uri.parse("smsto:"));
      smsIntent.setType("vnd.android-dir/mms-sms");
      smsIntent.putExtra("address"  , new String ("01234"));
      smsIntent.putExtra("sms_body"  , "Test ");
      
      try {
         startActivity(smsIntent);
         finish();
         Log.i("Finished sending SMS...", "");
      }
      catch (android.content.ActivityNotFoundException ex) {
         Toast.makeText(MainActivity.this, 
         "SMS faild, please try again later.", Toast.LENGTH_SHORT).show();
      }
   }
   
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
   }
}

 

Following will be the content of res/layout/activity_main.xml file −

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent" 
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin" 
   tools:context=".MainActivity">
   
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="SMS Sending Example"
      android:id="@+id/textView"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:textSize="30dp" />
      
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Andro Development "
      android:id="@+id/textView2"
      android:layout_below="@+id/textView"
      android:layout_centerHorizontal="true"
      android:textSize="30dp"
      android:textColor="#ff14be3c" />
      
   <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imageView"
      android:src="@drawable/abc"
      android:layout_marginTop="48dp"
      android:layout_below="@+id/textView2"
      android:layout_centerHorizontal="true" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Compose SMS"
      android:id="@+id/button"
      android:layout_below="@+id/imageView"
      android:layout_alignRight="@+id/textView2"
      android:layout_alignEnd="@+id/textView2"
      android:layout_marginTop="54dp"
      android:layout_alignLeft="@+id/imageView"
      android:layout_alignStart="@+id/imageView" />
      
</RelativeLayout>

Following will be the content of res/values/strings.xml to define two new constants

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="app_name">androdevelopment</string>
   <string name="action_settings">Settings</string>
</resources>

 

Following is the default content of AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.androdevelopment"
   android:versionCode="1"
   android:versionName="1.0" >
   
   <uses-sdk
      android:minSdkVersion="8"
      android:targetSdkVersion="22" />
      
   <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      
      <activity
         android:name="com.example.androdevelopment.MainActivity"
         android:label="@string/app_name" >
         
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
         
      </activity>
      
   </application>
</manifest>

 

Let’s make an effort to run our androdevelopment app (Android Sending SMS app). I suppose you have linked your actual Android os Mobile device with your personal computer. To perform the app from Android studio, available one of your project’s activity and click Run Eclipse Run icon android sending sms from the toolbar. Prior to starting your application, Google android studio will screen following window to choose a choice where you want to perform your Android request.

android sending sms

Choose your mobile device as a choice and then check your mobile device that may display following display screen:

 

android sending sms

Now use Compose Text message button to unveiling Android built-in Text clients which is given below:

android sending sms

You can adjust either of the given default areas and lastly use send Text button to send your Text message to the stated recipient.

 

 


Do You Have Any Question About This Tutorial? Ask Question Now On Andro Development & Get Answer Instantly. No Signup/Login Needed