RunCodes
Programming & Tech

How to Send Free SMS using Android?[With Source Code]

9 2,902

As you all know guyz, the era that we are living is an era of internet, so everyone is hanging on their PC all the time. Everyday we are sending some SMS to our friends in order to inform something to them. So, if there is some method that can send SMS for free, everyone gonna use that method and loved it. Today, in this tutorial we are gonna discuss about that method using Android Programming. And also if you wanna send the message to your friends without displaying your number and name, this tutorial is for you! sometime we are in the mode of cracking jokes and wanna fool your friends or sending some threatening message(only for entertainment purpose otherwise it will be illegal) you can use this method.

“This tutorial may be very useful for those who want to integrate SMS features in their project which is developed in Android”.

For sending SMS using SMSAPI approach: you have to do two task:

1.   Selecting Carrier

2.    Writing android code

STEPS:

[sociallocker]

1. we are selecting txtlocal as our carrier. So, All you have do is go to http://www.txtlocal.co.uk and there you need to create one account. after creating account. (you need to fill up some of your information and txtlocal will send you a verification email into your email and verify your account [watch the video for detail]). After completing this step, go http://api.txtlocal.com/docs/sendsms and from the select language option choose java(because android is as same java) and then copy the code and edit as in the following manner or simply copy the following code but you need to provide your own api key when you run this project.

2. For writing the code follow the following steps:

I. I am assuming that you have Android Studio in your PC, open Android Studio, go to file>new>new project; give application name click next, select phone and tablets;click next, select empty activity click next and finally click finish.

Now you project will open in android studio. Go to res>layout>activity_main.xml. Double click on activity_mail.xml file, select design section. And drag and drop four edittext and one button and change their properties as shown in the video. OR simply copy following code; keep in mind that you need to change the package name and application name.

II. Designing Code:


<?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:id="@+id/activity_main"
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="ran.com.sendsmsusingapi.MainActivity">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:id="@+id/txtapi"
android:textSize="22sp"
android:inputType="textMultiLine"
android:hint="ENTER API KEY"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_marginTop="18dp"
android:id="@+id/txtsender"
android:hint="ENTER SENDER NAME"
android:textSize="22sp"
android:layout_below="@+id/txtapi"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_marginTop="27dp"
android:id="@+id/txtphone"
android:hint="ENTER PHONE WITH COUNTRY CODE"
android:textSize="18sp"
android:layout_below="@+id/txtsender"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<EditText
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:inputType="textMultiLine"
android:ems="10"
android:id="@+id/txtmess"
android:maxHeight="10dp"
android:maxLines="10"
android:hint="ENTER MESSAGE HERE"
android:minHeight="180dp"
android:scrollbars="vertical"
android:gravity="top"
android:textSize="22sp"
android:width="1dp"
android:layout_below="@+id/txtphone"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="25dp" />

<Button
android:text="SEND"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnsend"
android:textSize="22sp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="11dp" />

</RelativeLayout>

III. We are sending SMS from online so we need to give permission to your app to use internet. So, goto your manifest.xml file and add the following permission before the application tag but below the manifest tag.

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

IV. Now goto java>yourpackagename.com.applicationname in my case java>ran.com.sendsmsusingapi>MainActivity

V. Inside the class declare the following variable as:

EditText _txtapi, _txtmess, _txtsender, _txtnum; Button _btnsend;

VI. Inside the onCreate method initialize each control as:

_txtapi = (EditText)findViewById(R.id.txtapi);
_txtmess=(EditText)findViewById(R.id.txtmess);
_txtsender=(EditText)findViewById(R.id.txtsender);
_txtnum=(EditText)findViewById(R.id.txtphone);
_btnsend=(Button)findViewById(R.id.btnsend);

VII. Now create listening event of the button and write the following piece of code.


_btnsend.setOnClickListener(new View.OnClickListener() {
 @Override public void onClick(View v) {
 try {
 // Construct data String apiKey = "apikey=" + _txtapi.getText().toString();
 String message = "&message=" + _txtmess.getText().toString();
 String sender = "&sender=" + _txtsender.getText().toString();
 String numbers = "&numbers=" + _txtnum.getText().toString();

// Send data HttpURLConnection conn = (HttpURLConnection) new URL("https://api.txtlocal.com/send/?").openConnection();
 String data = apiKey + numbers + message + sender;
 conn.setDoOutput(true);
 conn.setRequestMethod("POST");
 conn.setRequestProperty("Content-Length", Integer.toString(data.length()));
 conn.getOutputStream().write(data.getBytes("UTF-8"));
 final BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
 final StringBuffer stringBuffer = new StringBuffer();
 String line;
 while ((line = rd.readLine()) != null) {
 //stringBuffer.append(line); Toast.makeText(getApplicationContext(), "the message is "+line, Toast.LENGTH_LONG).show();
 }
 rd.close();

//return stringBuffer.toString(); } catch (Exception e) {
 //System.out.println("Error SMS "+e); Toast.makeText(getApplicationContext(), "the error message is"+e, Toast.LENGTH_LONG).show();
 //return "Error "+e; }
 }
});


VIII. After completing above step, if you run your project either in any emulator or Android Studio builtin ADB, you will see the os.NetworkOnMainThreadException because You should almost always run network operations on a thread or as an asynchronous task. So, Create the thread and write the above code inside that thread or strict the thread and write the following code inside the onCreate method but outsize the listening event of button.

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

IX. Now Login to the your txtlocal account, goto setting>API KEY and then click on create API Key and then simply copy that created key.

[/sociallocker]

That’s it guyz. Now RUN Your apps in any AVD and enter the value in corresponding field and click Send.

if you have any confusion watch the following video:

 

 

Show Comments (9)