Android Reading Inbox SMS

Sample source code for reading inbox sms


import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;

public class SMSRead extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView view = new TextView(this);
Uri uriSMSURI = Uri.parse("content://sms/inbox");
Cursor cur = getContentResolver().query(uriSMSURI, null, null, null,null);
String sms = "";
while (cur.moveToNext()) {
sms += "From :" + cur.getString(2) + " : " + cur.getString(11)+"\n";
}
view.setText(sms);
setContentView(view);
}
}


Add below permission to AndroidManifest.xml


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

30 comments :

  1. Hi
    Nice tutorial save my time .But i have one question how we read the new incoming message.I want to read the new incoming messages any idea about this

    ReplyDelete
    Replies
    1. Make BroadCast Receiver for incoming message.

      Delete
  2. Hi
    Please tell me how to view the new incoming message in android

    ReplyDelete
  3. Setup the Android Manifest with the permission and the Intent Receiver












    Code a simple Intent Listener
    package org.apache.sms;

    import android.app.NotificationManager;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentReceiver;
    import android.os.Bundle;
    import android.provider.Telephony;
    import android.util.Log;
    import android.telephony.gsm.SmsMessage;

    public class SMSApp extends IntentReceiver {
    private static final String LOG_TAG = "SMSApp";

    /* package */ static final String ACTION =
    "android.provider.Telephony.SMS_RECEIVED";

    public void onReceiveIntent(Context context, Intent intent) {
    if (intent.getAction().equals(ACTION)) {
    StringBuilder buf = new StringBuilder();
    Bundle bundle = intent.getExtras();
    if (bundle != null) {
    SmsMessage[] messages = Telephony.Sms.Intents.getMessagesFromIntent(intent);
    for (int i = 0; i < messages.length; i++) {
    SmsMessage message = messages[i];
    buf.append("Received SMS from ");
    buf.append(message.getDisplayOriginatingAddress());
    buf.append(" - ");
    buf.append(message.getDisplayMessageBody());
    }
    }
    Log.i(LOG_TAG, "[SMSApp] onReceiveIntent: " + buf);
    NotificationManager nm = (NotificationManager) context.getSystemService(
    Context.NOTIFICATION_SERVICE);

    nm.notifyWithText(123, buf.toString(),
    NotificationManager.LENGTH_LONG, null);

    }
    }

    private void appendData(StringBuilder buf, String key, String value) {
    buf.append(", ");
    buf.append(key);
    buf.append('=');
    buf.append(value);
    }
    }

    ReplyDelete
  4. can u please tell us how to run this code in sdk at our desktop please?

    ReplyDelete
  5. wonder & great . I really thankful to you

    ReplyDelete
  6. uses-permission has a printing mistake. it should be like this:

    ReplyDelete
  7. help me!
    i want hide display when my phone reciever incoming call.how to do it ? please.
    everybody

    ReplyDelete
  8. i want detail code to block sms using the keyword given in textbox and to block sms from selected number in contact list...anybody help plz..

    ReplyDelete
  9. hi everybody
    i am working my project.When have an incoming phone call, the screen has not changed its.How to do it (can somebody give me a little code for this problem ) ? please help me!

    ReplyDelete
  10. Appication force quits as i run
    vinodhkumarsampath@gmail.com

    ReplyDelete
  11. It's work perfectly
    Thank you for sharing ;)
    Android The Best!

    ReplyDelete
  12. please tell me code to read data from mmssms.db

    ReplyDelete
  13. i need some help
    i am making a projrect in which i received the GPS coordinates througth sms into the google andriod phone .
    my problem is i want to put the the coordinates from inbox of the andriod phone in to the google map through some application.
    Means the app should continuously get the coordinates from inbox and insert the coordinates for real time tracking plzzzzzz plzzzzzzzzzz plzzzzzzzzzzz any one help me out

    ReplyDelete
  14. i need some help
    i am making a projrect in which i received the GPS coordinates througth sms into the google andriod phone .
    my problem is i want to put the the coordinates from inbox of the andriod phone in to the google map through some application.
    Means the app should continuously get the coordinates from inbox and insert the coordinates for real time tracking plzzzzzz plzzzzzzzzzz plzzzzzzzzzzz any one help me out
    plz mail me at
    Email: omar3228422552@gmail.com

    ReplyDelete
  15. Hi i need some help .
    In my application when i get new inbox message with content please call/help automatically read the incoming message phone number and show an alert with message content for ready to coll to that number or not.please help me

    ReplyDelete
  16. please tell me how can I run this code in android sdk...?

    ReplyDelete
  17. Nice Thread Tutorial, i am new in android , its help me a lot ...

    I have got some good links

    here
    at androidexample.com
    Incomming SMS Broadcast Receiver

    ReplyDelete
  18. i am working on robotic,few dificulties i have found in by robot, it is working on android app,please help me in developing application for "sms reading and sending "

    ReplyDelete
  19. your code is a gr8 gr8 gr8 help for me... i need little more help that if i want time stamp of each msg received how i can attain that????

    ReplyDelete
  20. I am getting below error can u plz help me how can i reslove this below error

    java.lang.SecurityException: Neither user 10045 nor current process has android.permission.MODIFY_PHONE_STATE.

    ReplyDelete
  21. it shows error "android.database.Cursor
    Note: This element has no attached Javadoc and the Javadoc could not be found in the attached source." how to fix it

    ReplyDelete
  22. this code is not working for htc phone...

    ReplyDelete
  23. Hello All,

    I want to make a project in android like reading the message from the inbox and display full message into once it is clicked on particular message. I have used the listview to display the message but it is display the entire content on listview but i want it to display the full msg one it is clicked on particular msg not all at the same time.Like how we are using the mobile inbox msg to read the msg. Please do help if you know.

    Thank you..
    Aarav

    ReplyDelete
  24. can any one give me a code for the restore sms backup...???
    thanx in advance

    ReplyDelete
  25. Hi,
    I want to get body of SMS when I click on one. I have tried an approach simmilar to contact picker. Here is my code :

    public class Decrypt extends Fragment {
    Button loadButton;
    EditText smsDisplay;
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View v = inflater.inflate(R.layout.decrypt, null);
    // Button loadButton=(Button)v.findViewById(R.id.)
    loadButton=(Button)v.findViewById(R.id.buttonLoad);
    smsDisplay=(EditText)v.findViewById(R.id.editTextLoad);
    loadButton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

    Intent sendIntent = new Intent(Intent.ACTION_VIEW);
    sendIntent.setData(Uri.parse("sms:"));
    //sendIntent.setType(Sm)
    //sendIntent.putExtra("sms_body","");

    startActivityForResult(sendIntent,2);
    }
    });


    return v;

    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    try{
    // if (resultCode == Activity.RESULT_OK) {
    Uri ur = data.getData();
    Cursor c = getActivity().getContentResolver().query(ur, null, null,
    null, null);
    if (c.moveToFirst()) {
    String s = c
    .getString(c
    .getColumnIndex("body"));
    System.out.println(s);
    Toast.makeText(getActivity(), s, Toast.LENGTH_LONG).show();
    smsDisplay.setText(s);
    }
    }catch(Exception e){
    e.printStackTrace();
    }
    // }
    }
    }

    It opens a Inbox like when I open original sms app in anroid, but when I click on one message it doesnt copy body of that sms in my edittext. Please couul you look at my code and find mistake?

    Thanks a lot for your time

    ReplyDelete
  26. Hi friends;


    I want to make a app like whatsapp plz help . all data let say communication , messaging , status

    Thanks

    ReplyDelete
  27. Hi

    using this code i'm getting messages are displaying with mobile numbers instead of names stored for number's in contact book. Can anyone help me with that ?

    ReplyDelete
  28. Wow, finally i got good code, it was worked well, i had searching many sites about this code, Thanks for sharing, keep updating

    Android app development company

    ReplyDelete