ads

Breaking News

28.Sending SMS in android

Sending And Receiving SMS in android
Sending And Receiving SMS in android


Introduction

◼Sending and receiving SMS messages is considered as one of the most economical and popular modes of communication.

◼Android provides us 2 ways to send SMS which are

           □Using SmsManager class
           □Using Implicit Intent



Understanding SmsManager class


◼The class SmsManager comes from the package android.telephony and can be used to send SMS messages.

◼The character limit for a single message is 160 characters. Text messages above this size must be broken into chunks.

◼To give us control over this, the SmsManager class allows us to break a message into “parts”, and returns a list of them.



Step Needed For Sending SMS Using SmsManager class


To send SMS programmatically there are 5 steps needed:

1.Obtain an object of SmsManager class

This is done by calling the factory method of SmsManager class called getDefault() whose prototype is:

public static SmsManager getDefault( )


2.Divide the message into multiple fragments

This is done by calling the method divideMessage( ) of the SmsManager object. The prototype of this method is

ArrayList<String> divideMessage (String text)

This method accepts the message to be sent as argument and divides it into several fragments, none
bigger than the maximum SMS message size.


3.Check whether message is a single-part or multipart message

Since we got an ArrayList from divideMessage( ), we can check it’s size by using the size( ) method to know whether it is a single-part message or multi-part message


4.Use appropriate method for sending the message depending on whether it is a single-part or multi-part message

if there is only one part, the message is short enough to send directly, so we use the sendTextMessage() method. It’s prototype is:

public final void sendTextMessage (String destinationAddress, String address, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)

Following are the five arguments to the sendTextMessage() method:

destinationAddress — Phone number of the recipient

scAddress — SMS center address; use null for default SMSC

text — Content of the SMS message

sentIntent — Pending intent to invoke when the message is sent

deliveryIntent — Pending intent to invoke when the message has been delivered

Otherwise, if the message is a multi-part message wehave to call the method sendMultipartTextMessage( ) whose prototype is:

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


5.Specify the permission in manifest file

To send SMS messages, our applications must specify the SEND_SMS uses-permission. To request this permission, we add it to the manifest as shown below:

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


Example: Write an app which accepts a message and a mobile number and sends the message to the given number.


Sending And Receiving SMS in android




Solution :


Complete Java Code


public class MainActivity extends AppCompatActivity {
 private EditText ed1,ed2;
 private Button btn;
 private String dest,msg;
 private final int MY_PERMISSIONS_REQUEST_SEND_SMS = 1;
@Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
        ed1=(EditText)findViewById(R.id.ed1);
        ed2=(EditText)findViewById(R.id.ed2);
        btn=(Button)findViewById(R.id.btn);
  btn.setOnClickListener(new View.OnClickListener() {
  @Override
   public void onClick(View v) {
    dest=ed1.getText().toString();
    msg=ed2.getText().toString();
    if(dest.isEmpty()||msg.isEmpty()) {
     Toast.makeText(MainActivity.this,"Please input both fields",Toast.LENGTH_LONG).show();
     return;
    }
    if (ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.SEND_SMS)!= PackageManager.PERMISSION_GRANTED) {
     Toast.makeText(MainActivity.this,"No permission given for sending SMS.Requesting the user. . .",Toast.LENGTH_LONG).show();
     ActivityCompat.requestPermissions(MainActivity.this,
     new String[]{Manifest.permission.SEND_SMS},
     MY_PERMISSIONS_REQUEST_SEND_SMS);
     return;
    }
    sendMessage();
   } 
  });
 }
@Override
 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[ ] grantResults) {
  switch (requestCode) {
   case MY_PERMISSIONS_REQUEST_SEND_SMS:
   if (grantResults.length > 0&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    sendMessage();
   } 
  }
 }
 public void sendMessage(){
  SmsManager obj=SmsManager.getDefault();
  ArrayList<String> messageList=obj.divideMessage(msg);
  if(messageList.size()==1) {
   obj.sendTextMessage(dest,null,msg,null,null);
  }
  else{
   obj.sendMultipartTextMessage(dest,null,messageList,null,null);
  }
 }
}





Sending SMS Using Intent


◼Using the SmsManager class, we can send SMS messages from within our application without the need to involve the built-in Messaging application. 

◼However, sometimes it would be easier if we could simply invoke the built-in Messaging application and let it handle sending the message.

◼To activate the built-in Messaging application from within our application, we can use an Intent object with the MIME type "vnd.android-dir/mms-sms"



Steps Required

◼To activate the built-in Messaging application from within our application we need to do the following:


1.Create an implicit intent with ACTION_VIEW

2.Call the putExtra( ) method of an Intent object passing it “address” as the key and destination phone number as the value.

3.Call the putExtra( ) method of Intent object passing it “sms_body” as key and the message to send as the value

4.Call the setType( ) method of Intent object passing it the MIME type string as “vnd.android-dir/mms-sms”

5.Call the startActivity( ) method passing it the Intent object as argument




The Code


Intent i;
i= new
Intent(android.content.Intent.ACTION_VIEW);

i.putExtra(“address”, “9826686245”);

i.putExtra(“sms_body”, “Hello from android”);

i.setType(“vnd.android-dir/mms-sms”);

startActivity(i);








Thanks . . .

⬜⬜⬜

No comments