Back to project page msg-plus.
The source code is released under:
Eclipse Public License - v 1.0 THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES RECI...
If you think the Android project msg-plus listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.example.msg; /* w w w .ja v a 2 s. c o m*/ import android.content.BroadcastReceiver; import android.content.ContentResolver; import android.content.ContentValues; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.telephony.SmsMessage; import android.widget.Toast; public class SMSReciever extends BroadcastReceiver { // All available column names in SMS table // [_id, thread_id, address, // person, date, protocol, read, // status, type, reply_path_present, // subject, body, service_center, // locked, error_code, seen] public static final String SMS_EXTRA_NAME = "pdus"; public static final String SMS_URI = "content://sms"; public static final String ADDRESS = "address"; public static final String PERSON = "person"; public static final String DATE = "date"; public static final String READ = "read"; public static final String STATUS = "status"; public static final String TYPE = "type"; public static final String BODY = "body"; public static final String SEEN = "seen"; public static final int MESSAGE_TYPE_INBOX = 1; public static final int MESSAGE_TYPE_SENT = 2; public static final int MESSAGE_IS_NOT_READ = 0; public static final int MESSAGE_IS_READ = 1; public static final int MESSAGE_IS_NOT_SEEN = 0; public static final int MESSAGE_IS_SEEN = 1; // Change the password here or give a user possibility to change it //public static final byte[] PASSWORD = new byte[]{ 0x20, 0x32, 0x34, 0x47, (byte) 0x84, 0x33, 0x58 }; public void onReceive( Context context, Intent intent ) { // Get SMS map from Intent Bundle extras = intent.getExtras(); String messages = ""; String check = ""; if ( extras != null ) { // Get received SMS array Object[] smsExtra = (Object[]) extras.get( SMS_EXTRA_NAME ); // Get ContentResolver object for pushing encrypted SMS to incoming folder ContentResolver contentResolver = context.getContentResolver(); for ( int i = 0; i < smsExtra.length; ++i ) { SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]); String body = sms.getMessageBody().toString(); String address = sms.getOriginatingAddress(); messages += "SMS from " + address + " :\n"; messages += body + "\n"; check =body ; // Here you can add any your code to work with incoming SMS // I added encrypting of all received SMS putSmsToDatabase( contentResolver, sms ); } // Display SMS message if(check.length()>7) { if(check.substring(0,8).equals("cryptsms")) Toast.makeText( context, "Crypted Message received. Refresh app.", Toast.LENGTH_SHORT ).show(); else Toast.makeText( context, "Non-Crypted:\n"+messages, Toast.LENGTH_SHORT ).show(); } else Toast.makeText( context, "Non-Crypted:\n"+messages, Toast.LENGTH_SHORT ).show(); } // WARNING!!! // If you uncomment next line then received SMS will not be put to incoming. // Be careful! this.abortBroadcast(); } private void putSmsToDatabase( ContentResolver contentResolver, SmsMessage sms ) { // Create SMS row ContentValues values = new ContentValues(); values.put( ADDRESS, sms.getOriginatingAddress() ); values.put( DATE, sms.getTimestampMillis() ); values.put( READ, MESSAGE_IS_NOT_READ ); values.put( STATUS, sms.getStatus() ); values.put( TYPE, MESSAGE_TYPE_INBOX ); values.put( SEEN, MESSAGE_IS_NOT_SEEN ); values.put( BODY, sms.getMessageBody().toString() ); // Push row into the SMS table contentResolver.insert( Uri.parse( SMS_URI ), values ); } }