Home > Tutorials > Flash SMS

Flash SMS

Flash SMS is the first app that I have created for the Android.

I wrote the FLASH SMS app for use by volunteer firefighters in the Ballarat Fire Brigade. It is a partner application to a turnout avaiability app that I have created for our brigade. The turnout avaiability application receives SMS messages from volunteers. The vols send either Yes, No, or Call to the app and it changes a flag next to their name to show if they are avaiable or not. See Below:

The Flash SMS app makes this process much faster.

 

BFB Turnout Availability App

 

Flash SMS Feature Graphic

My Java programming skills are very limited at the moment but I am happy to share the code for this app as it may be of use.

Please note that my Java coding style is very rough at the moment as I have lots to learn about good code style.

flashSMS.Java

package au.id.rupert.FlashSMS001;
// Version flashSMS 1.1
// Purpose to send one of 3 predefined SMS messages to a predefined phone number

// This is the version I am publishing 30 Dec 2012 17:30

import java.util.regex.Matcher;
import java.util.regex.Pattern;

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.content.SharedPreferences;

import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class flashSMS extends Activity {

Button btnSendYes;
Button btnSendNo;
Button btnSendCall;
Button btnConfiguration;

// Initialise variables
String strPhoneNumber = "";
String strNoMessage = "";
String strCallMessage = "";
String strYesMessage = "";

// set up SharedPreferences
private SharedPreferences prefs;
private static final String PHONE_NUMBER_KEY = "phoneNumber";
private static final String NO_MESSAGE_KEY = "noMessage";
private static final String CALL_MESSAGE_KEY = "callMessage";
private static final String YES_MESSAGE_KEY = "yesMessage";
private String prefName = "MyPref";

IntentFilter intentFilter;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dwalker); // Load screen layout with default button texts

// Toast.makeText(getApplicationContext(), "flashSMS 1.1 ", Toast.LENGTH_SHORT).show();

//---get the SharedPreferences object---
prefs = getSharedPreferences(prefName, MODE_PRIVATE);

//---load the SharedPreferences object---
SharedPreferences prefs = getSharedPreferences(prefName, MODE_PRIVATE);
// Toast.makeText(getApplicationContext(), prefs.getString(PHONE_NUMBER_KEY, ""), Toast.LENGTH_SHORT).show();

// Restore the saved PhoneNumber string
strPhoneNumber = prefs.getString(PHONE_NUMBER_KEY, "");
// Toast.makeText(getApplicationContext(),"Restored strPhoneNumber: " + strPhoneNumber, Toast.LENGTH_SHORT).show();

// Restore the saved No Message string
strNoMessage = prefs.getString(NO_MESSAGE_KEY, "");
// Try to set button text
// Button mButton=(Button)findViewById(R.id.btnSendNo);
// mButton.setText(strNoMessage);
// Toast.makeText(getApplicationContext(),"strNoMessage: " + strNoMessage, Toast.LENGTH_SHORT).show();

// Restore the saved Call Message string
strCallMessage = prefs.getString(CALL_MESSAGE_KEY, "");
// Toast.makeText(getApplicationContext(),"strCallMessage: " + strCallMessage, Toast.LENGTH_SHORT).show();

// Restore the saved Yes Message string
strYesMessage = prefs.getString(YES_MESSAGE_KEY, "");
// Toast.makeText(getApplicationContext(),"strYesMessage: " + strYesMessage, Toast.LENGTH_SHORT).show();

//---intent to filter for SMS messages received---
intentFilter = new IntentFilter();
intentFilter.addAction("SMS_RECEIVED_ACTION");

btnSendYes = (Button) findViewById(R.id.btnSendYes);
btnSendNo = (Button) findViewById(R.id.btnSendNo);
btnSendCall = (Button) findViewById(R.id.btnSendCall);
btnConfiguration = (Button) findViewById(R.id.btnConfiguration);

// Start of btnSendNo %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
btnSendNo.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
// Error Checking using a regular expression
// see: http://stackoverflow.com/questions/6358380/phone-number-validation-android

String NUMBER_PATTERN = "^[+]?[0-9]{10,13}$";

try {

Pattern pattern = Pattern.compile(NUMBER_PATTERN);
Matcher matcher = pattern.matcher(strPhoneNumber);

if (matcher.matches()) {

// The Phone Number looks OK so proceed to checking if there is a Red message to send
if (strNoMessage.equals(""))
{
// There is no message text
Toast.makeText(getApplicationContext(),"Please configure the 'Red Message'.", Toast.LENGTH_SHORT).show();
}
else {
// It's OK to send the SMS
sendSMS(strPhoneNumber, strNoMessage);
Toast.makeText(getApplicationContext(),strPhoneNumber + "\n" + strNoMessage, Toast.LENGTH_LONG).show();
}
}
else {
// Invalid Number
Toast.makeText(getApplicationContext(),"Invalid Phone Number", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(),"Configure SMS messages", Toast.LENGTH_SHORT).show();
}
}catch(Exception ex){
}
}
});// End of btnSendNo

// Start of btnSendCall %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
btnSendCall.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{

// Error Checking using a regular expression
// see: http://stackoverflow.com/questions/6358380/phone-number-validation-android

String NUMBER_PATTERN = "^[+]?[0-9]{10,13}$";

try {

Pattern pattern = Pattern.compile(NUMBER_PATTERN);
Matcher matcher = pattern.matcher(strPhoneNumber);

if (matcher.matches()) {

// The Phone Number looks OK so proceed to checking if there is a yellow message to send
if (strCallMessage.equals(""))
{
// There is no 'Yellow message' text
Toast.makeText(getApplicationContext(),"Please configure the 'Yellow Message'.", Toast.LENGTH_SHORT).show();
}
else {
// It's OK to send the SMS
sendSMS(strPhoneNumber, strCallMessage);
Toast.makeText(getApplicationContext(),strPhoneNumber + " \n" + strCallMessage, Toast.LENGTH_LONG).show();
}
}
else {
// Invalid Number
Toast.makeText(getApplicationContext(),"Invalid Phone Number", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(),"Configure SMS messages", Toast.LENGTH_SHORT).show();
}
}catch(Exception ex){
}
}
});// End of btnSendCall

// Start of btnSendYes %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
btnSendYes.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{


// Error Checking using a regular expression
// see: http://stackoverflow.com/questions/6358380/phone-number-validation-android

String NUMBER_PATTERN = "^[+]?[0-9]{10,13}$";

try {

Pattern pattern = Pattern.compile(NUMBER_PATTERN);
Matcher matcher = pattern.matcher(strPhoneNumber);

if (matcher.matches()) {

// The Phone Number looks OK so proceed to checking if there is a Green message to send
if (strCallMessage.equals(""))
{
// There is no 'Call message' text
Toast.makeText(getApplicationContext(),"Please configure the 'Green Message'.", Toast.LENGTH_SHORT).show();
}
else {
// It's OK to send the SMS
sendSMS(strPhoneNumber, strYesMessage);
Toast.makeText(getApplicationContext(),strPhoneNumber + "\n" + strYesMessage, Toast.LENGTH_LONG).show();
}
}
else {
// Invalid Number
Toast.makeText(getApplicationContext(),"Invalid Phone Number", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(),"Configure SMS messages", Toast.LENGTH_SHORT).show();
}
}catch(Exception ex){
}
}
}); // End of btnSendYes


// Start of btnConfiguration
btnConfiguration = (Button) findViewById(R.id.btnConfiguration);
btnConfiguration.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
//Starting a new Intent to switch screens
Intent Config = new Intent(getApplicationContext(), Configuration.class);

// starting new activity
startActivity(Config);
}
}); // End of btnConfiguration

}

@Override
protected void onResume() {
//---register the receiver---
//registerReceiver(intentReceiver, intentFilter);
super.onResume();

//---get the SharedPreferences object---
prefs = getSharedPreferences(prefName, MODE_PRIVATE);

//---load the SharedPreferences object---
SharedPreferences prefs = getSharedPreferences(prefName, MODE_PRIVATE);

// Toast.makeText(getApplicationContext(), prefs.getString(PHONE_NUMBER_KEY, ""), Toast.LENGTH_SHORT).show();

// Restore the saved PhoneNumber string
strPhoneNumber = prefs.getString(PHONE_NUMBER_KEY, "");
// Toast.makeText(getApplicationContext(),"Resumed! strPhoneNumber: " + strPhoneNumber, Toast.LENGTH_SHORT).show();

// Restore the saved No Message string
strNoMessage = prefs.getString(NO_MESSAGE_KEY, "");
// Toast.makeText(getApplicationContext(),"Resumed! strNoMessage: " + strNoMessage, Toast.LENGTH_SHORT).show();

// Restore the saved Call Message string
strCallMessage = prefs.getString(CALL_MESSAGE_KEY, "");
// Toast.makeText(getApplicationContext(),"Resumed! strCallMessage: " + strCallMessage, Toast.LENGTH_SHORT).show();

// Restore the saved Yes Message string
strYesMessage = prefs.getString(YES_MESSAGE_KEY, "");
// Toast.makeText(getApplicationContext(),"Resumed! strYesMessage: " + strYesMessage, Toast.LENGTH_SHORT).show();


// Change the text on the buttons to match the saved text from preferences
// Try to set button text
Button NoButton=(Button)findViewById(R.id.btnSendNo);
NoButton.setText(strNoMessage);

Button CallButton=(Button)findViewById(R.id.btnSendCall);
CallButton.setText(strCallMessage);

Button YesButton=(Button)findViewById(R.id.btnSendYes);
YesButton.setText(strYesMessage);

}

@Override
protected void onPause() {
//---unregister the receiver---
//unregisterReceiver(intentReceiver);
super.onPause();

}

@Override
protected void onDestroy() {
//---unregister the receiver---
super.onPause();
}

//---sends an SMS message to another device---
private void sendSMS(String phoneNumber, String message)
{
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";

PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
new Intent(SENT), 0);

PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);

//---when the SMS has been sent---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent OK",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure. SMS not sent.",
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(), "Flight mode turned on!",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));

//---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered OK",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));

SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}

}

 

Manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="au.id.rupert.FlashSMS001"
   android:versionCode="2"
   android:versionName="1.1" >

   <uses-sdk
      android:minSdkVersion="8"
      android:targetSdkVersion="15" />
   <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
   <application
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
     android:theme="@style/AppTheme" >
   <activity
      android:name=".flashSMS"
      android:label="@string/title_activity_dwalker" >
      <intent-filter>
         <action android:name="android.intent.action.MAIN" />

         <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
   </activity>

      <activity android:name=".Configuration"></activity>

   </application>

</manifest>

 

 

 

 

 

 


APA citation:
Russell, R. (2016, July 04, 02:03 pm). What are fractals?
     Retrieved October 22, 2017, from
     http://www.rupert.id.au/fractals/index.php

Last refreshed: October 22 2017. 05:36.12 pm

rupert dot russell at acu dot edu dot au Support Wikipedia

Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License.


2134 Visits since September 30, 2004