Search in sources :

Example 1 with SmsMessage

use of android.telephony.gsm.SmsMessage in project android-demos by novoda.

the class SmsReceiver method onReceive.

@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();
    }
}
Also used : SmsMessage(android.telephony.gsm.SmsMessage) Bundle(android.os.Bundle)

Example 2 with SmsMessage

use of android.telephony.gsm.SmsMessage in project 360-Engine-for-Android by 360.

the class SmsBroadcastReceiver method onReceive.

/**
 * Implementation of the {@link BroadcastReceiver#onReceive} function.
 * Decodes Hessian from SMS text and broadcasts the data to the engines
 *
 * @param context The current context of the People application
 * @param intent Intent containing the SMS data
 */
@Override
public void onReceive(Context context, Intent intent) {
    LogUtils.logD("SmsBroadcastReceiver.onReceive - Begin of sms receiver. \n" + intent.toString());
    Bundle bundle = intent.getExtras();
    Object[] messages = (Object[]) bundle.get("pdus");
    SmsMessage[] smsMessage = new SmsMessage[messages.length];
    for (int n = 0; n < messages.length; n++) {
        try {
            smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
            LogUtils.logD("SmsBroadcastReceiver.onReceive - SMS no [" + n + "]bytes: " + new String(smsMessage[n].getUserData()));
            HessianDecoder hd = new HessianDecoder();
            Hashtable<String, Object> ht = hd.decodeHessianByteArrayToHashtable(smsMessage[n].getUserData());
            if (ht != null) {
                LogUtils.logD("SmsBroadcastReceiver.onReceive - Decoded hashtable: " + ht.toString());
                Hashtable<?, ?> codeTable = (Hashtable<?, ?>) ht.get("e");
                String code = (String) codeTable.get("r");
                LogUtils.logD("SmsBroadcastReceiver.onReceive - Activation code: " + code);
                Intent in = new Intent(ACTION_ACTIVATION_CODE);
                in.putExtra("code", code);
                context.sendBroadcast(in);
            } else {
                String code = "No code in SMS";
                LogUtils.logD("SmsBroadcastReceiver.onReceive - Code not in sms");
                Intent in = new Intent(ACTION_ACTIVATION_CODE);
                in.putExtra("code", code);
                context.sendBroadcast(in);
            }
        } catch (IOException e) {
            LogUtils.logE("SmsBroadcastReceiver.onReceive() " + "IOException while decoding SMS Message.", e);
        }
    }
}
Also used : Bundle(android.os.Bundle) Hashtable(java.util.Hashtable) Intent(android.content.Intent) IOException(java.io.IOException) SmsMessage(android.telephony.gsm.SmsMessage) HessianDecoder(com.vodafone360.people.service.utils.hessian.HessianDecoder)

Aggregations

Bundle (android.os.Bundle)2 SmsMessage (android.telephony.gsm.SmsMessage)2 Intent (android.content.Intent)1 HessianDecoder (com.vodafone360.people.service.utils.hessian.HessianDecoder)1 IOException (java.io.IOException)1 Hashtable (java.util.Hashtable)1