Android Open Source - txttr S M S Receiver






From Project

Back to project page txttr.

License

The source code is released under:

MIT License

If you think the Android project txttr listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.izgoy.txttr;
/*w  w  w.  j a  v  a2  s. co m*/
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.telephony.SmsMessage;

import java.util.HashMap;
import java.util.Map;

public class SmsReceiver extends BroadcastReceiver {

    public SmsReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {

        SharedPreferences settings = TxttrApplication.getSettings(context);
        if (!settings.getBoolean(TxttrApplication.PREF_ENABLED, false)) {
            return;
        }

        Map<String, String> messages = retrieveMessages(intent);
        if (messages == null) {
            return;
        }
        for (Map.Entry<String, String> message : messages.entrySet()) {
            SmsHandlerService.startHandleSMS(context, message.getKey(), message.getValue());
        }

    }

    /**
     * From https://code.google.com/p/gtalksms/source/browse/src/com/googlecode/gtalksms/receivers/SmsReceiver.java
     */
    private static Map<String, String> retrieveMessages(Intent intent) {
        Map<String, String> msg = null;
        SmsMessage[] msgs;
        Bundle bundle = intent.getExtras();

        if (bundle != null && bundle.containsKey("pdus")) {
            Object[] pdus = (Object[]) bundle.get("pdus");

            if (pdus != null) {
                int nbrOfpdus = pdus.length;
                msg = new HashMap<String, String>(nbrOfpdus);
                msgs = new SmsMessage[nbrOfpdus];

                // There can be multiple SMS from multiple senders, there can be a maximum of nbrOfpdus different senders
                // However, send long SMS of same sender in one message
                for (int i = 0; i < nbrOfpdus; i++) {
                    msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);

                    String originatinAddress = msgs[i].getOriginatingAddress();

                    // Check if index with number exists
                    if (!msg.containsKey(originatinAddress)) {
                        // Index with number doesn't exist
                        // Save string into associative array with sender number as index
                        msg.put(msgs[i].getOriginatingAddress(), msgs[i].getMessageBody());

                    } else {
                        // Number has been there, add content but consider that
                        // msg.get(originatinAddress) already contains sms:sndrNbr:previousparts of SMS,
                        // so just add the part of the current PDU
                        String previousparts = msg.get(originatinAddress);
                        String msgString = previousparts + msgs[i].getMessageBody();
                        msg.put(originatinAddress, msgString);
                    }
                }
            }
        }

        return msg;
    }

}




Java Source Code List

com.izgoy.txttr.MainActivity.java
com.izgoy.txttr.SMSHandlerService.java
com.izgoy.txttr.SMSReceiver.java
com.izgoy.txttr.TxttrApplication.java
com.izgoy.txttr.UserManager.java