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; //from w w w. j av a 2 s. com import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.preference.PreferenceManager; import android.provider.BaseColumns; import android.provider.ContactsContract; import android.support.v4.app.NotificationCompat; import android.support.v4.app.TaskStackBuilder; import android.telephony.SmsManager; import java.util.ArrayList; public class SmsTools { private static int MAX_SMS_MESSAGE_LENGTH = 160; private SmsTools() { // static class } /** * Send an SMS message to another device */ public static boolean sendSMS(Context context, Message message, String phoneNumber) { String messageText = message.getText(); Intent sentIntent = new Intent(context, SmsSentReceiver.class); sentIntent.putExtra(Settings.KEY_SOURCE, phoneNumber); sentIntent.putExtra(Settings.KEY_MESSAGE, messageText); sentIntent.putExtra(Settings.KEY_TIME, message.getTime()); Intent deliveredIntent = new Intent(context, SmsDeliveredReceiver.class); deliveredIntent.putExtra(Settings.KEY_SOURCE, phoneNumber); deliveredIntent.putExtra(Settings.KEY_MESSAGE, messageText); deliveredIntent.putExtra(Settings.KEY_TIME, message.getTime()); PendingIntent piSent = PendingIntent.getBroadcast(context, 0, sentIntent, PendingIntent.FLAG_CANCEL_CURRENT); PendingIntent piDelivered = PendingIntent.getBroadcast(context, 0, deliveredIntent, PendingIntent.FLAG_CANCEL_CURRENT); SmsManager smsManager = SmsManager.getDefault(); try { final int length = messageText.length(); if (length > MAX_SMS_MESSAGE_LENGTH) { // split sms into multiple parts ArrayList<String> messageList = smsManager.divideMessage(messageText); //smsManager.sendMultipartTextMessage(phoneNumber, null, messageList, piSent, piDelivered); for (String messagePart : messageList) { smsManager.sendTextMessage(phoneNumber, null, messagePart, piSent, piDelivered); } } else { // send sms as one message smsManager.sendTextMessage(phoneNumber, null, messageText, piSent, piDelivered); } } catch (NullPointerException e) { e.printStackTrace(); return false; } return true; } /** * Send SMS notification to user */ public static void sendSmsNotification(Context context, String title, String text, String ticker) { sendSmsNotification(context, null, title, text, ticker); } /** * Send SMS notification to user */ public static void sendSmsNotification(Context context, String source, String title, String text, String ticker) { NotificationCompat.Builder builder = new NotificationCompat.Builder(context); setBuilder(context, builder, title, text, ticker); Intent resultIntent; if (source == null) { // Creates an explicit intent for MainActivity resultIntent = new Intent(context, MainActivity.class); } else { // Creates an explicit intent for MainActivity with source information to load fragment resultIntent = new Intent(context, MainActivity.class); resultIntent.putExtra(Settings.KEY_SOURCE, source); } // set pending intent with/without TaskStackBuilder based on Android version setPendingIntent(context, resultIntent, builder); NotificationManager notificationManager = (NotificationManager)context.getSystemService( Context.NOTIFICATION_SERVICE); notificationManager.notify(0, builder.build()); } private static void setBuilder(Context context, NotificationCompat.Builder builder, String title, String text, String ticker) { builder .setSmallIcon(R.drawable.ic_launcher) .setContentTitle(title) .setContentText(text) .setTicker(ticker) .setAutoCancel(true) .setLights(0xffffffff, 300, 1000); boolean vibrate = PreferenceManager.getDefaultSharedPreferences(context).getBoolean( "notifications_new_message_vibrate", true); if (vibrate) { builder.setDefaults(Notification.DEFAULT_VIBRATE); } String sound = PreferenceManager.getDefaultSharedPreferences(context).getString("notifications_new_message_ringtone", ""); if (sound != null && !sound.equals("")) { builder.setSound(Uri.parse(sound)); } } /** * Set pending intent based on API version */ private static void setPendingIntent(Context context, Intent resultIntent, NotificationCompat.Builder builder) { TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); // Adds the result intent with its parent(s) behind it stackBuilder.addNextIntentWithParentStack(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_CANCEL_CURRENT); builder.setContentIntent(resultPendingIntent); // Set delete intent Intent intent = new Intent(context, NotificationReceiver.class); PendingIntent deletePendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); builder.setDeleteIntent(deletePendingIntent); } public static void cancelNotifications(Context context) { NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.cancelAll(); } /** * Find contact by number and return contact display name */ public static String getContactDisplayNameByNumber(ContentResolver contentResolver, String number) { Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number)); String name = null; Cursor contactLookup = contentResolver.query( uri, new String[] { BaseColumns._ID, ContactsContract.PhoneLookup.DISPLAY_NAME }, null, null, null); try { if (contactLookup != null && contactLookup.getCount() > 0) { contactLookup.moveToNext(); name = contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME)); } } finally { if (contactLookup != null) { contactLookup.close(); } } return name; } }