How to send SMS in android using Intent?

In this article, we are going to learn about the intent to use it for sending SMS on a cell phone number.
Submitted by Manu Jemini, on February 25, 2018

In the example below, we are going to use the Intent and Bundle class to make a SMS and send it programmatically from your app with intent.

To implement this in your program you will need to import: android.content.Intent, android.net.Uri in your java file.

Then create a single layout file with a single Button. Button should be made like this:

<Button
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:text="Send SMS"
	android:id="@+id/button"
	android:onClick="sms"
	android:layout_alignParentBottom="true"
	android:layout_centerHorizontal="true"
	android:layout_marginBottom="114dp" />

In the Button above we have put an onClick property and set it to SMS function which is in the java File. We assign on click function directly like this: android:onClick="sms".

First, we will make an Intent object of type Intent.ACTION_VIEW. Next, we set data to that object with the URI parse method.

    I.setData(Uri.parse("smsto:"));
    I.setType("vnd.android-dir/mms-sms");
    I.putExtra("address", new String ("1234567890"));

At last, we take permission and start call.

1) Java file:

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;


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

    }

    public void sms(View v)
    {
        Log.i("Sending SMS","");
        Intent I =new Intent(Intent.ACTION_VIEW);

        I.setData(Uri.parse("smsto:"));
        I.setType("vnd.android-dir/mms-sms");
        I.putExtra("address", new String ("1234567890"));
        I.putExtra("sms_body","Enter your Sms here..");

        try
        {
          startActivity(I);
            finish();
            Log.i("Sms Send","");
        }
        catch(Exception e)
        {
            Toast.makeText(MainActivity.this,"Sms not send",Toast.LENGTH_LONG).show();
        }
    }
}

2) XML file:

<?xml version="1.0" encoding="utf-8"?>
<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="com.example.hp.myapplication.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send SMS"
        android:id="@+id/button"
        android:onClick="sms"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="114dp" />

</RelativeLayout>

3) Android Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hp.myapplication">
<uses-permission android:name="android.permission.CALL_PHONE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Output

Android - send SMS using Intent 1
Android - send SMS using Intent 2


Related Tutorials




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.