6

I want to develop an app which tracks the sent/received SMSs. I mean, when a user sends a message from its device, the message detail should be saved to a table provided by me. Similarly, when the device receives any SMS, then that also should be saved into a table provided by me.

Note that the user uses the default message application of Android to send message. I mean I am not integrating my application with the default message application. I just need to keep track of all messages sent from that application as Android keeps track of all sent messages in Sent Message folder of its Message app.

How would I do this? Please help me. I don't need answers, but I need some clue to do it.

Please don't suggest to read the messages from Inbox and Outbox etc. Because I want to save the messages when user sends/ receives it. Not after sends or receives it.

Chandra Sekhar
  • 18,914
  • 16
  • 84
  • 125
  • 2
    What have you tried and what's the problem you are facing? Just keep it simple. – Andro Selva Jun 06 '12 at 08:01
  • @AndroSelva I don't have any clue how to do that. I don't want answer, but I want some clue. – Chandra Sekhar Jun 06 '12 at 08:04
  • @Mido I don't want to read the messages from Inbox and show it. I want to save the message when user sends it. – Chandra Sekhar Jun 06 '12 at 08:08
  • I think this [Post][1] has code snippets of what you're looking for. [1]: http://stackoverflow.com/questions/848728/how-can-i-read-sms-messages-from-the-inbox-programmatically-in-android – dotmido Jun 06 '12 at 08:09

2 Answers2

10

This is easy to do with a broadcast Receiver write in your Manifest:

edit: seems only to work for SMS_RECEIVED see this thread

<receiver android:name=".SMSReceiver"  android:enabled="true">
 <intent-filter android:priority="1000">
      <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
      <action android:name="android.provider.Telephony.SMS_SENT"/>
 </intent-filter>
</receiver>

And the Permission:

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

Then Create the Receiver llike:

public class SMSReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
       if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
    //do something with the received sms        
       }else  if(intent.getAction().equals("android.provider.Telephony.SMS_SENT")){
            //do something with the sended sms
     }
  }
}

To handle a incoming sms might look like:

Bundle extras = intent.getExtras();
Object[] pdus = (Object[]) extras.get("pdus");
for (Object pdu : pdus) {
        SmsMessage msg = SmsMessage.createFromPdu((byte[]) pdu);
        String origin = msg.getOriginatingAddress();
        String body = msg.getMessageBody();
....
}

If you want to prevent a sms to be pushed in the commen InBox, you can achieve this with:

abortBroadcast();
Community
  • 1
  • 1
2red13
  • 11,197
  • 8
  • 40
  • 52
2

Please do not call abortBroadcast() you will prevent other apps receiving the SMS_RECEIVED ordered broadcast. This is bad behavior this is not what android is about. I dont understand why google even lets developer abort broadcasts of system intents like SMS.

Noam Segev
  • 56
  • 4
  • 2
    This abortBroadcast thing allow us to make softwares to block incoming calls or sms. I think this is a good thing that can be used as a bad thing, like a lot of other stuffs. People can use a pen to kill, lets stop to build pens. – Lucas Eduardo Feb 03 '15 at 17:35
  • I agree. Its very usefull for a lot of good Features.You should not Abort every incoming SMS, but my Receiver is able to decide: This SMS is for me, so abort broadcasting, otherwise let it pass through – 2red13 May 31 '16 at 09:37