Android Open Source - smsgcm-client Sms Receiver






From Project

Back to project page smsgcm-client.

License

The source code is released under:

Apache License

If you think the Android project smsgcm-client 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.omgren.apps.smsgcm.client;
// w w  w.j a va  2s  . co  m
import static com.omgren.apps.smsgcm.client.CommonUtilities.displayMessage;
import static com.omgren.apps.smsgcm.client.ServerUtilities.uploadMessage;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.PhoneLookup;
import android.telephony.SmsMessage;
import android.util.Log;

import com.omgren.apps.smsgcm.common.SmsMessageDummy;

public class SmsReceiver extends BroadcastReceiver {
  private static final String TAG = "SmsReceiver";

  @Override
    public void onReceive(Context context, Intent intent){
      Bundle bundle = intent.getExtras();
      Object messages[] = (Object[]) bundle.get("pdus");

      // go through sms messages
      for( int n = 0; n < messages.length; n++ ){
        SmsMessage sms = SmsMessage.createFromPdu((byte[])messages[n]);
        SmsMessageDummy sms_ = new SmsMessageDummy();

        // get sender and message
        sms_.message = sms.getDisplayMessageBody();
        sms_.address = sms.getDisplayOriginatingAddress();
        sms_.name = sms_.address;
        sms_.time = sms.getTimestampMillis();

        // lookup name from phone number
        if( !sms.isEmail() )
          sms_.name = lookupName(context, sms_.address);

        Log.i(TAG, "received sms form " + sms_.name + " saying " + sms_.message);
        displayMessage(context, context.getString(R.string.recv_sms, sms_.name, sms_.message));

        // send the message to the server
        uploadMessage(context, sms_);
      }

    }

  public String lookupName(Context context, String phoneNumber){
    String name = phoneNumber;

    String[] projection = new String[]{ PhoneLookup.DISPLAY_NAME };
    Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
    Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);

    try {
      if( cursor.moveToNext() ){
        name = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
      }
    } finally {
      cursor.close();
    }

    return name;
  }
}




Java Source Code List

com.omgren.apps.smsgcm.client.CertException.java
com.omgren.apps.smsgcm.client.CertUtilities.java
com.omgren.apps.smsgcm.client.CommonUtilities.java
com.omgren.apps.smsgcm.client.GCMIntentService.java
com.omgren.apps.smsgcm.client.HttpUtilities.java
com.omgren.apps.smsgcm.client.MainActivity.java
com.omgren.apps.smsgcm.client.ServerUtilities.java
com.omgren.apps.smsgcm.client.SettingsActivity.java
com.omgren.apps.smsgcm.client.SmsReceiver.java
com.omgren.apps.smsgcm.client.SmsSender.java
com.omgren.apps.smsgcm.common.SmsMessageDummy.java