Add SMS gateway to your android application
There are many websites or companies who provide an API to allow you to send a humongous amount of SMS from your application. Today, I will give you an overview of how to do it with an example.
Step 1:
Get an sms API from this website: https://www.textlocal.in/
- First, sign up on this website.
- Copy your API key.
Step 2:
Make an android project and add two edit text in your xml file to get mobile number and message that needs to be sent.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/number"
android:inputType="number"
android:maxLines="1"
android:hint="Mobile Number"
android:padding="10dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/message"
android:inputType="text"
android:maxLines="1"
android:hint="Message"
android:padding="10dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SEND"
android:textColor="@color/colorPrimary"
android:id="@+id/send"
android:padding="10dp"/>
</LinearLayout>
MainActivity.java
private EditText number;
private EditText message;
private Button send;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
number = (EditText)findViewById(R.id.number);
message = (EditText)findViewById(R.id.message);
send = (Button)findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String num = number.getText().toString();
String msg = message.getText().toString();
if(num.length() == 10){
if(!TextUtils.isEmpty(num) && !TextUtils.isEmpty(msg)){
snms_sender hn = new snms_sender(num,msg);
hn.execute();
}else{
Toast.makeText(MainActivity.this, "Please input all the feilds.", Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(MainActivity.this, "Please enter a correct number.", Toast.LENGTH_SHORT).show();
}
}
});
}
Now, the layout is done. We will have to make a class which will connect to API and send sms.
sms_sender.java
snms_sender extends AsyncTask<String, String, String> {
public String num;
public String msg;
public snms_sender(String num,String msg){
this.num = num;
this.msg = msg;
}
@Override
protected String doInBackground(String... strings) {
try {
// Construct data
String apiKey = "apikey=" + "0QF1KAyJPU0-ah0j81MddpJBrtO4WsOmDoPCnUrEu4";
String message = "&message=" + msg;
String sender = "&sender=" + "TXTLCL";
String numbers = "&numbers=" + "91" + num;
// Send data
HttpURLConnection conn = (HttpURLConnection) new URL("https://api.textlocal.in/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);
}
rd.close();
return stringBuffer.toString();
} catch (Exception e) {
System.out.println("Error SMS "+e);
return "Error "+e;
}
}
}
Now, you are ready to send SMS with your application.
Like this, if you liked it.
Comment, if you have any query.