Back to project page android-textlater.
The source code is released under:
Apache License
If you think the Android project android-textlater listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * Copyright (C) 2013 The Android Open Source Project *//w w w .j a v a 2 s . c om * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.michael.feng.textlater; import android.app.Activity; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.telephony.SmsManager; import android.util.Log; import java.util.ArrayList; public class SendActivity extends Activity { public String messageId = ""; private static final String SMS_SENT = "SMS_SENT"; private static final String SMS_DELIVERED = "SMS_DELIVERED"; private PendingIntent sentIntent = null; private PendingIntent deliveryIntent = null; private BroadcastReceiver sentReceiver = null; private BroadcastReceiver deliveryReceiver = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_send); sendTask(contructMessage()); Intent in = new Intent(this, MainActivity.class); startActivity(in); } // Construct message object private Message contructMessage() { String textNumber = ""; String textWhen = ""; String textContent = ""; Intent intent = getIntent(); if (intent.hasExtra("textNumber")) textNumber = intent.getStringExtra("textNumber"); if (intent.hasExtra("textWhen")) textWhen = intent.getStringExtra("textWhen"); if (intent.hasExtra("textContent")) textContent = intent.getStringExtra("textContent"); Message message = new Message(); message.setTextNumber(textNumber); message.setTextWhen(textWhen); message.setTextContent(textContent); return message; } // Parse and Send message private void sendTask(Message message) { String contactsNumbersStr = message.getTextNumber().trim(); String contentStr = message.getTextContent(); String[] contactNumbers = null; if (contactsNumbersStr.indexOf(";") > 0) { contactNumbers = contactsNumbersStr.split(";"); } else { contactNumbers = new String[1]; contactNumbers[0] = contactsNumbersStr; } if (null != contactNumbers && contactNumbers.length > 0) { for (String contactNumber : contactNumbers) { // Validate contactNumber didn't have letter for (int j = 0; j < contactNumber.length(); j++) { if (Character.isLetter(contactNumber.charAt(j))) { return; } } sendMessage(contactNumber, contentStr); } } } private void sendMessage(String contact, String message) { sentIntent = PendingIntent.getBroadcast(this, 0, new Intent(SMS_SENT), 0); deliveryIntent = PendingIntent.getBroadcast(this, 0, new Intent(SMS_DELIVERED), 0); // ---when the SMS has been sent--- sentReceiver = new BroadcastReceiver() { @Override public void onReceive(Context arg0, Intent arg1) { switch (getResultCode()) { case Activity.RESULT_OK: Log.d("SENT", "OK"); //Toast.makeText(getBaseContext(), "SMS sent", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_GENERIC_FAILURE: //Toast.makeText(getBaseContext(), "Generic failure", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_NO_SERVICE: //Toast.makeText(getBaseContext(), "No service", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_NULL_PDU: //Toast.makeText(getBaseContext(), "Null PDU", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_RADIO_OFF: //Toast.makeText(getBaseContext(), "Radio off", Toast.LENGTH_SHORT).show(); break; } } }; registerReceiver(sentReceiver, new IntentFilter(SMS_SENT)); // ---when the SMS has been delivered--- deliveryReceiver = new BroadcastReceiver() { @Override public void onReceive(Context arg0, Intent arg1) { switch (getResultCode()) { case Activity.RESULT_OK: Log.d("RETURN", "OK"); //Toast.makeText(getBaseContext(), "SMS delivered", Toast.LENGTH_SHORT).show(); break; case Activity.RESULT_CANCELED: Log.d("RETURN", "FAIL"); //Toast.makeText(getBaseContext(), "SMS not delivered", Toast.LENGTH_SHORT).show(); break; } } }; registerReceiver(deliveryReceiver, new IntentFilter(SMS_DELIVERED)); SmsManager textManager = SmsManager.getDefault(); // Use Message length mod 160, get message count int textCount = message.length() / 160; if (textCount > 0 && message.length() > (160 * textCount)) { textCount = textCount + 1; } // Send single or multi SMS base on textCount got above if (textCount > 0) { ArrayList<String> parts = textManager.divideMessage(message); ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>(); ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>(); for (int i = 0; i < textCount; i++) { sentIntents.add(sentIntent); deliveryIntents.add(deliveryIntent); } textManager .sendMultipartTextMessage(contact, null, parts, sentIntents, deliveryIntents); } else { textManager.sendTextMessage(contact, null, message, sentIntent, deliveryIntent); } } @Override protected void onDestroy() { if (null != sentReceiver) { unregisterReceiver(sentReceiver); } if (null != deliveryReceiver) { unregisterReceiver(deliveryReceiver); } super.onDestroy(); } }