Saedawke Saedawke - 1 year ago 45
Android Question

Android How to Develop SMS App, which will receive users messages and send as JSON

I Need to develop an app that reads users SMS messages immediately at arrives users device, then extracts and send to web server as JSON data.
I'm new to android development.
the messages we'll track will come from +252898 and these messages are common in content except some extra information so the thing we need is that extra content to be send to our web app with JSON data.
sample message which come from +252898.

enter image description here

Answer Source

There are 2 components involved, the logic when a sms is received and parsing the data, and the logic to send the data to your web app.

To get a working "hook" when a SMS is received, you will want to build a broadcast receiver with a SMS_RECEIVED intent.

In your application, create a new BroadcastReceiver called MySMSReceiver, and add the Receiver to the manifest:

<receiver android:name=".MySMSReceiver">   
         <action android:name="android.provider.Telephony.SMS_RECEIVED" />

Additionally, add the permissions to the manifest to be able to receive and read the SMS:

<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>

Now here is the partial code you will have for the receiver:

public class MySMSReceiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {


Now when you receive an SMS on your phone, your broadcast receiver will run its onReceive method because your intent filter specifically specifies SMS_RECIVED intents.

With SMS_RECEIVED intents, (the parameter received on the onReceive method), there will be a couple extras within the intent that you can read. Within onReceive, you can add logic similar to the following:

Bundle bundle = intent.getExtras();
if (bundle != null) {    
    Object[] pdus = (Object[]) bundle.get("pdus");
    for (int i = 0; i < pdus.length; i++) {
        SmsMessage message = SmsMessage.createFromPdu((byte[]) pdus[i]);
        String senderNumber = message.getDisplayOriginatingAddress();
        String messageContent = message.getDisplayMessageBody();

As above, you will then have to make sure that the message is of your interest by checking the senderNumber or messageContent. A lot of application tries regex pattern matching to make sure that the current received message is of the app's interest.

Now you will need to construct your JSON object and POST it to your server.

Import several classes to your Receiver and create a Post method with the post logic to construct a json object to send to your web app:

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;

public void Post(string number, string message){
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("your web api url here");
            JSONObject jsonObject = new JSONObject();
            jsonObject.accumulate("number", number);
            jsonObject.accumulate("message", message);
            StringEntity se = new StringEntity(jsonObject.ToString());
            httpPost.setHeader("Accept", "application/json");
            httpPost.setHeader("Content-type", "application/json");

        } catch (Exception e) {

Of course now if you want to read the response of the POST, you can read the return value of the httpclient.execute(httpPost).

Now the logic is complete, you just need to put the pieces together to the way that fits your need.