There are two way to send SMS in Android
1. Using Intent to send SMS
2. Using SmsManager to send SMS
* Using Intent to send SMS
We can use Intent to send SMS by calling default SMS functionality of the Android. It's easy to use because we don’t have to do anything special.1. Create XML file in layout folder "res/layout/activity_main.xml".
<LinearLayout 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:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="15dp" > <Button android:id="@+id/btnComposeSMS" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="Open Compose SMS" /> </LinearLayout> </LinearLayout>
2. Create on activity java file "MainActivity.java"
import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; 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 { private Button btnOpenComposeSMS; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnOpenComposeSMS = (Button) findViewById(R.id.btnComposeSMS); //Open Compose SMS Button Client Event Code here btnOpenComposeSMS.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent smsIntent = new Intent(Intent.ACTION_VIEW); smsIntent.setData(Uri.parse("smsto:")); smsIntent.setType("vnd.android-dir/mms-sms"); smsIntent.putExtra("address" , new String ("0000000000")); smsIntent.putExtra("sms_body" , "Open Comopse SMS using Intent"); try { startActivity(smsIntent); } catch (android.content.ActivityNotFoundException ex) { Toast.makeText(MainActivity.this,"SMS faild, please try again later.", Toast.LENGTH_SHORT).show(); } } }); } }
3.Add your activity class in AndroidMenifest.xml file
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.limbani.sendsms" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".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>
4. Screenshot
*Using SmsManager to send SMS
In Android provide SmsManager API to send SMS programmatically. see below example code.1. Create XML file in layout folder "res/layout/activity_main.xml".
<LinearLayout 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:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="15dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Number" /> <EditText android:id="@+id/edt_number" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:ems="10" android:inputType="phone" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="Message" /> <EditText android:id="@+id/edt_message" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:gravity="top" android:minLines="5" android:text="Send SMS Demo Application" /> <Button android:id="@+id/btnSend" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="Send" /> </LinearLayout> </LinearLayout>
2. Create on activity java file "MainActivity.java"
package com.limbani.sendsms; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; 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 { private Button btnSend; private EditText edtNumber, edtMessage; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnSend = (Button) findViewById(R.id.btnSend); edtNumber = (EditText) findViewById(R.id.edt_number); edtMessage = (EditText) findViewById(R.id.edt_message); //Send Button Client Event Code here btnSend.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String number = edtNumber.getText().toString().trim(); String message = edtMessage.getText().toString().trim(); if(number.length() <= 0){ Toast.makeText(MainActivity.this,"Please enter Number", Toast.LENGTH_SHORT).show(); } else if (message.length() <= 0) { Toast.makeText(MainActivity.this,"Please enter message", Toast.LENGTH_SHORT).show(); } else { try { SmsManager smsManager = SmsManager.getDefault(); smsManager.sendTextMessage(number, null, message, null, null); Toast.makeText(getApplicationContext(), "SMS successfully sent", Toast.LENGTH_LONG).show(); } catch (Exception e) { Toast.makeText(getApplicationContext(), "SMS faild, please try again.", Toast.LENGTH_LONG).show(); e.printStackTrace(); } } } }); } }
3. Add your activity class in AndroidManifest.xml file.
Add user pemission
"
<uses-permission android:name="android.permission.SEND_SMS"/>"
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.limbani.sendsms" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <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=".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>
4. Screenshot
Download APK file Here
No comments:
Post a Comment