DEVELOPMENT


sms send

The objective of this article is to demonstrate how to include SMS sending facillity in your android application.

THE SITUATION

BUILD.GRADLE
apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.example.tut.tut_10_1"
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.0'
}


AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tut.tut_10_1" >
<uses-permission android:name="android.permission.SEND_SMS" />
<application
android:allowBackup="true"
android:icon="@mipmap/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>

MainActivity.java
package com.example.tut.tut_10_1;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.BackgroundColorSpan;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

EditText pn, mess;
String[] arr;
String allres;
Spannable spanna,spannableText;
Spanned spa;
TextView tv;
String textString,tstr;
CharSequence charSequence;
int i,start,end;
SpannableStringBuilder ssb;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tv = (TextView)findViewById(R.id.tv);
pn = (EditText)findViewById(R.id.pnumber);
mess = (EditText)findViewById(R.id.mess);
tstr = pn.getText().toString();
textString = "";
spa = new SpannableString(textString);
tv.setVisibility(View.GONE);
}

public void onClick(View v) {

allres = tv.getText().toString();
arr = allres.split(" ");

for (i = 1; i < arr.length; i++) {
sendSMS(arr[i], mess.getText().toString());
}
}
public void onClickAdd(View v) {

tstr = pn.getText().toString();

if(tstr.trim().length() > 0)
{
spanna = new SpannableString(tstr);
spanna.setSpan(new BackgroundColorSpan(Color.LTGRAY), 0, tstr.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void updateDrawState(TextPaint ds) {

ds.setUnderlineText(false);

}

@Override
public void onClick(final View textview) {

TextView tvs = (TextView) textview;
charSequence = tvs.getText();
if (charSequence instanceof Spannable) {
spannableText = (Spannable) charSequence;
start = spannableText.getSpanStart(this);
end = spannableText.getSpanEnd(this);
ssb = new SpannableStringBuilder(spannableText);
ssb.delete(start - 1, end);// start - 1 so that whitespace too get deleted
}
new AlertDialog.Builder(MainActivity.this)
.setTitle("Remove")
.setMessage("Are you sure you want to remove this number?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
spa = ssb;
int c=0;
for(int d=0;d<ssb.length();d++)
{
char cs = ssb.charAt(d);

if(cs == ' ')
{

}
else
{
c=1;
}
}
if(c==0)
{
tv.setText("");
tv.setVisibility(View.GONE);

}
else
{
tv.setText(ssb);
}

}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {

}
})
.show();
}
};

spanna.setSpan(clickableSpan, 0, tstr.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

spa = (Spanned) TextUtils.concat(spa, " ", spanna);
tv.setText(spa);
tv.setMovementMethod(LinkMovementMethod.getInstance());
tv.setVisibility(View.VISIBLE);


pn.setText("");
}
}
private void sendSMS(String phoneNumber, String message)
{
PendingIntent sentPI;
String SENT = "SMS_SENT";
sentPI = PendingIntent.getBroadcast(this, 0,new Intent(SENT), 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, null);
}

}
res/layout/activity_main.xml - XML graphical layout for MainActivity.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@color/white">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
android:gravity="center">
<TextView
android:id="@+id/tv"
android:textSize="25px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="2px"
android:paddingTop="4px"
android:gravity="center_vertical|center_horizontal"/>
<EditText
android:id="@+id/pnumber"
android:layout_width="200dp"
android:layout_height="50dp"
android:gravity="center_vertical|center_horizontal"/>
<Button
android:id="@+id/addrec"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/cl"
android:textColor="@color/white"
android:textSize="16px"
android:text="Add more recepints"
android:gravity="center_vertical|center_horizontal"
android:onClick="onClickAdd"/>
<EditText
android:id="@+id/mess"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:gravity="center_vertical|center_horizontal"/>
<Button
android:id="@+id/sndsms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/cl"
android:textColor="@color/white"
android:textSize="16px"
android:text="Send Message(SMS)"
android:gravity="center_vertical|center_horizontal"
android:onClick="onClick" />
</LinearLayout>
</ScrollView>

</LinearLayout>

res/values/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="cl">#FF0000</color>
<color name="white">#ffffff</color>
</resources>


Leave a Comment  
Download Code(82)





All rights reserved, the content is copyrighted to the author.

RELATED POST