Back to project page MySms.
The source code is released under:
Apache License
If you think the Android project MySms 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.henningta.mysms; // w ww. j a v a2s. c om import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.preference.PreferenceManager; import android.telephony.SmsMessage; import android.util.Log; import java.util.Calendar; public class SmsReceiver extends BroadcastReceiver { private static final String TAG = SmsReceiver.class.getSimpleName(); @Override public void onReceive(Context context, Intent intent) { WakeLocker.acquire(context); Bundle extras = intent.getExtras(); if (extras == null) { WakeLocker.release(); return; } DatabaseHandler dbHandler = new DatabaseHandler(context); Object[] smsextras = (Object[])extras.get("pdus"); for (Object msg : smsextras) { SmsMessage smsmsg = SmsMessage.createFromPdu((byte[])msg); // get information from sms String source = smsmsg.getOriginatingAddress(); String messageText = smsmsg.getMessageBody(); //long time = smsmsg.getTimestampMillis(); // TODO: time sometimes one hour off... (sporadic) long time = Calendar.getInstance().getTimeInMillis(); Log.i(TAG, "SMS from " + source + " : " + messageText); // find current conversation or add it to the list Conversation conversation = dbHandler.getConversation(source); if (conversation == null) { conversation = new Conversation(context.getContentResolver(), source); dbHandler.addConversation(conversation); } // add message to conversation Message smsMessage = new Message(source, messageText, time, Message.STATUS_RECEIVED); dbHandler.addMessage(smsMessage); // update conversation conversation.setHasNewMessages(true); conversation.setTime(time); dbHandler.updateConversation(conversation); // send notification to user boolean notificationsEnabled = PreferenceManager.getDefaultSharedPreferences(context).getBoolean("notifications_new_message", true); if (notificationsEnabled) { String title = conversation.getName(); String text = messageText; if (dbHandler.containsNotificationSource(source)) { dbHandler.incrementNotificationCount(source); } else { dbHandler.addNotificationSource(source); } if (dbHandler.getNotificationSources().size() > 1) { title = dbHandler.getNotificationCount() + " new messages"; text = ""; for (String name : dbHandler.getNotificationNames()) { if (dbHandler.getNotificationNames().indexOf(name) == dbHandler.getNotificationNames().size() - 1) { text += name; } else { text += name + ", "; } } SmsTools.sendSmsNotification(context, title, text, messageText); } else { int count = dbHandler.getNotificationCount(source); if (count > 1) { text = count + " new messages"; } SmsTools.sendSmsNotification(context, source, title, text, messageText); } } // refresh main page Intent conversationIntent = new Intent(Settings.ACTION_CONVERSATION); context.sendBroadcast(conversationIntent); // refresh messages page Intent messageIntent = new Intent(Settings.ACTION_MESSAGE); messageIntent.putExtra(Settings.KEY_SOURCE, source); messageIntent.putExtra(Settings.KEY_MESSAGE, messageText); messageIntent.putExtra(Settings.KEY_TIME, time); context.sendBroadcast(messageIntent); } // clean up dbHandler.close(); WakeLocker.release(); } }