Blog Details

img
Design

How DO u SMs on an Android

Spoke Right / 19 Nov, 2023

Send SMS In Android

To send SMS in Android, either the SmsManager API or devices Built-in SMS application can be used, i.e., in Android, the Intent can be used to send SMS.

Syntax: SmsManager API:

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

Syntax: 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);

Example: Send SMS in android using Intent:

//Getting intent and PendingIntent instance  
Intent intent=new Intent(getApplicationContext(),MainActivity.class);  
PendingIntent pi=PendingIntent.getActivity(getApplicationContext(), 0, intent,0);  
 
//Get the SmsManager instance and call the sendTextMessage method to send message                 
SmsManager sms=SmsManager.getDefault();  
sms.sendTextMessage("123456789", null, "Hello World", pi,null);

Example:

activity_main.xml:

In the activity_main.xml file, we will drag two EditTexts, two Textviews, and a button from the palette.



 
    
 
    
 
    
 
    
 
 

AndroidManifest.xml:

In the AndroidManifest.xml file, we will write the SEND_SMS permission code.

Syntax:


File: AndroidManifest.xml:



    android:versionCode="1"
    android:versionName="1.0" >
 
    
 
    
 
    
 
    
        
            
                
 
                
            
        
    
 

Activity class:(File: MainActivity.java)

In the MainActivity.java file, we will write the code to make the phone call via intent.

package com.example.radioapp;
 
import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
 
public class MainActivity extends Activity {
 
    EditText mobileno,message;
    Button sendsms;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        mobileno=(EditText)findViewById(R.id.editText1);
        message=(EditText)findViewById(R.id.editText2);
        sendsms=(Button)findViewById(R.id.button1);
 
        //Performing action on button click
        sendsms.setOnClickListener(new OnClickListener() {
 
            @Override
            public void onClick(View arg0) {
                String no=mobileno.getText().toString();
                String msg=message.getText().toString();
 
                //Getting intent and PendingIntent instance
                Intent intent=new Intent(getApplicationContext(),MainActivity.class);
                PendingIntent pi=PendingIntent.getActivity(getApplicationContext(), 0, intent,0);
 
                //Get the SmsManager instance and call the sendTextMessage method to send message
                SmsManager sms=SmsManager.getDefault();
                sms.sendTextMessage(no, null, msg, pi,null);
 
                Toast.makeText(getApplicationContext(), "Message Sent!!",
                        Toast.LENGTH_LONG).show();
            }
        });
    }
 
 
}

0 comments

Warning: PHP Startup: Unable to load dynamic library 'imagick.so' (tried: /usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so (/usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so: cannot open shared object file: No such file or directory), /usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so.so (/usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0