How to track messages in Android?

I want to develop an application that tracks sent / received SMS messages. I mean, when a user sends a message from his device, the detail of the message should be saved in the table provided by me. Similarly, when the device receives some kind of SMS message, this should also be saved in the table provided by me.

Please note that the user uses the default messaging app for Android to send the message. I mean that I do not integrate my application into the default message application. I just need to keep track of all the messages sent from this application, since Android keeps track of all sent messages in the Sent message folder in my messages application .

How should I do it? Please help me. I don't need answers, but I need a hint to do this.

Please do not offer to read messages from Inbox and Outbox, etc. Because I want to save messages when the user sends / receives it. Not after sending or receiving.

+5
source share
2 answers

This is easy to do with the broadcast receive entry in the manifest:

edit: it seems only 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 resolution:

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

Then create an llike receiver:

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 process incoming sms, it might look like this:

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 sms not to be pressed in commen InBox, you can achieve this with

abortBroadcast();
+9
source

Please do not call abortBroadcast () you will prevent other applications from receiving the ordered SMS_RECEIVED. This is bad behavior that Android is not talking about. I don’t understand why Google even allows the developer to interrupt broadcasts of system intentions, such as SMS.

+1
source

All Articles