0

Possible Duplicate:
Read all SMS from a particular sender

I want to know how to read sms and how split mobile number and message body. Please give me a sample code.

Community
  • 1
  • 1
JOHNZ
  • 1
  • 1
  • 5
  • 1
    I found this in the **Related** column [Read all SMS from a particular sender](http://stackoverflow.com/questions/10870230/read-all-sms-from-a-particular-sender). (Over there --> ) – Sam Sep 04 '12 at 17:57

1 Answers1

2

Code for the intent receiver that will read the SMS from intent received and show the message.

 public class SmsReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        //---get the SMS message passed in---
        Bundle bundle = intent.getExtras();        
        SmsMessage[] msgs = null;
        String str = "";            
        if (bundle != null)
        {
            //---retrieve the SMS message received---
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];            
            for (int i=0; i<msgs.length; i++){
                msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
                str += "SMS from " + msgs[i].getOriginatingAddress();                     
                str += " :";
                str += msgs[i].getMessageBody().toString();
                str += "\n";        
            }
            //---display the new SMS message---
            Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
        }                         
    }
}

And make sure to add this permission in your manifest file.

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

Also, msgs[i].getOriginatingAddress() gives you the sender of the SMS and you can check if this is your specific number or not. And then use msgs[i].getMessageBody().toString(); to show the body of the SMS.

This tutorial covers some of the aspects of your question.

Hope it helps.

Swayam
  • 16,294
  • 14
  • 64
  • 102
  • how do i call this onReceive() method in onCreate(). What is Context and Intent? – JOHNZ Sep 04 '12 at 18:45
  • It is a BroadcastReceiver and the onReceive() will be called automatically when any SMS is received. And it is through the intent that the sms data would be sent to this receiver class. The link to the tutorial would solve your questions. – Swayam Sep 04 '12 at 18:49
  • Is there any restrictions in order to proceed this in certain countries through service providers – JOHNZ Sep 04 '12 at 18:55
  • None that I know of, but I might be wrong. – Swayam Sep 04 '12 at 19:02
  • ok thanks for the quick reply, Also can you tell me how put this code to my application? – JOHNZ Sep 04 '12 at 19:19
  • You need to create a BroadcastReciver my friend. I have already given you the code. The link to the tutorial explains all the steps. And if you want I could give you more tutorials. – Swayam Sep 04 '12 at 19:20
  • I already created BroadcastReciver, I want to know what are the parameters to put in Context and Intent in onCreate() in main activity. – JOHNZ Sep 04 '12 at 19:22
  • onCreate() has only one parameter which is Bundle by default. You dont need to change it. The context and intent parameters are passed automatically to this receiver when it receives the broadcast message. – Swayam Sep 04 '12 at 19:25
  • I have create a SmsReceiver class and extend it by BroadcastReciver. after i have created a object from SmsReceiver class in main activity class like this. ` @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_smstest); SmsReceiver sms = new SmsReceiver(); sms.onReceive(context, intent); }` but it is get an error. plz Help me – JOHNZ Sep 04 '12 at 19:31
  • No no!!! You do not understand the concept of BroadcastReceivers! They are not to be called manually by you. They are called automatically by the application! So, just `public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_smstest); .....` would be fine. **NO NEED TO CALL THE BROADCAST RECEIVER YOURSELF** – Swayam Sep 04 '12 at 19:35
  • Have look at this http://mobiforge.com/developing/story/sms-messaging-android and http://www.androidcompetencycenter.com/2008/12/android-api-sms-handling/ – Swayam Sep 04 '12 at 19:39
  • is it enough to create a SmsReceiver class in seperetaly?? – JOHNZ Sep 04 '12 at 19:41
  • Yes, Just create the class. Implement whatever you want to do in it. Define the permission in the Manifest file and then you can forget about it. No need to call it yourself. However, you might want to call other activities from the BroadcastReceiver, which can be done by passing an intent for the new activity. – Swayam Sep 04 '12 at 19:43
  • its not working but not getting any error, should we have to call the activitiy class? please help me. – JOHNZ Sep 04 '12 at 19:57
  • Edit your question and add the full code. That would be really helpful for me to identify what you might be missing. – Swayam Sep 04 '12 at 19:59
  • And make sure to add this permission in your manifest file. ` ` – Swayam Sep 04 '12 at 19:59