How Can I Send Sms Using Android ?


Open Android Studio: Projects -> New Project -> Empty Views Activity -> Next -> Name: smsSend -> Language: Java -> Finish


[ TEST ] minSdk = 24 targetSdk = 34

In AndroidManifest.xml , one needs to include the below Permission, in order to access the Internet.


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


<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:dataExtractionRules="@xml/data_extraction_rules" android:fullBackupContent="@xml/backup_rules" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.SmsSend" tools:targetApi="31"> <activity android:name=".MainActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>

Next, Go to the activity_main.xml file, which represents the UI of the Project. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more details.


<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/titleI_id" android:layout_width="wrap_content" android:layout_height="38dp" android:layout_marginTop="44dp" android:fontFamily="serif-monospace" android:text="MSGP Server" android:textSize="34sp" android:textStyle="bold" app:layout_constraintBottom_toTopOf="@+id/status_id" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.162" /> <TextView android:id="@+id/label_id" android:layout_width="wrap_content" android:layout_height="32dp" android:layout_marginBottom="64dp" android:text="Return Response:" android:textSize="24sp" app:layout_constraintBottom_toTopOf="@+id/status_id" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.071" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/titleI_id" app:layout_constraintVertical_bias="1.0" /> <TextView android:id="@+id/status_id" android:layout_width="370dp" android:layout_height="191dp" android:layout_marginBottom="72dp" android:background="#FDFCF7" android:text="Remember to recharge your SIM card with a plan that includes 100 free SMS per day." android:textSize="20sp" app:layout_constraintBottom_toTopOf="@+id/button_id" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.487" app:layout_constraintStart_toStartOf="parent" /> <Button android:id="@+id/button_id" android:layout_width="159dp" android:layout_height="62dp" android:layout_marginBottom="136dp" android:text="Button" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>

Go to the MainActivity.java File and refer to the following code. Below is the code for the MainActivity.java File. Comments are added inside the code to understand the code in more detail.

Change the Authentication_id and Authentication_key.
Go to the home page, Click on the Authentication Key ID, and a page will display.
Copy and paste the 32-character Authentication ID and 32-character Authentication Key.


package com.example.smssend; import android.os.Bundle; import android.util.Log; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import org.json.JSONException; import org.json.JSONObject; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class MainActivity extends AppCompatActivity { TextView textView; Button button; private String msgObject; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.status_id); button = (android.widget.Button) findViewById(R.id.button_id); // -- Message Object // String mtype = "N"; // ["'N" = National phone number / "I" = International phone number ] String sender = "+919999999999"; String message = "This is a test message from Android..."; String auth_id = "Authentication_id"; String auth_key = "Authentication_key"; // Message Object --// button.setOnClickListener(v -> sendPostRequest(mtype, sender, message, auth_id, auth_key)); } public void sendPostRequest(String mtype, String sender, String message, String authId, String authKey) { Toast.makeText(MainActivity.this, "Return Response:", Toast.LENGTH_SHORT).show(); Thread thread = new Thread(() -> { try { URL url = new URL("https://msgpserver.com/api/"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8"); conn.setRequestProperty("Accept", "application/json"); conn.setDoOutput(true); conn.setDoInput(true); JSONObject jsonParam = new JSONObject(); jsonParam.put("mtype", mtype); jsonParam.put("sender", sender); jsonParam.put("message", message); jsonParam.put("auth_id", authId); jsonParam.put("auth_key", authKey); DataOutputStream os = new DataOutputStream(conn.getOutputStream()); os.writeBytes(jsonParam.toString()); os.flush(); os.close(); String nObj = String.valueOf(conn.getResponseCode()); if (nObj.equals("200")) { BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); msgObject = rd.readLine(); }else{ msgObject = "Server Down... "; } conn.disconnect(); rtnMsgObject(msgObject); } catch (Exception e) { e.printStackTrace(); } }); thread.start(); } private void rtnMsgObject(String msgObject) throws JSONException { Log.d("rtnMsgObject", "MsgObject: "+msgObject); textView.setText(msgObject); JSONObject jobj = new JSONObject(msgObject); String status = jobj.getString("status"); String login = jobj.getString("login"); String recharge = jobj.getString("recharge"); String host = jobj.getString("host"); String msg = jobj.getString("msg"); Log.d("rtnMsgObject", "status: "+status); Log.d("rtnMsgObject", "msg: "+msg); Log.d("rtnMsgObject", "recharge: "+recharge); Log.d("rtnMsgObject", "host: "+host); Log.d("rtnMsgObject", "login: "+login); } }

Return Success Response.


[ SUCCESS ] MsgObject: {'status': 'success', 'login': 'success', 'recharge': 'success', 'host': 'success', 'msg': 'success'}

Return Error Response.


[ ERROR ] MsgObject: {'status': 'success', 'login': 'success', 'recharge': 'success', 'host': 'error', 'msg': 'error'} MsgObject: {'status': 'success', 'login': 'success', 'recharge': 'expire', 'host': 'error', 'msg': 'error'} MsgObject: {'status': 'online', 'login': 'error', 'recharge': 'error', 'host': 'error', 'msg': 'error'}


Are you satisfied this Answer?