Pending Intents and BroadcastReceivers
It is possible to receive messages from the Android OS using BroadcastReceivers. You can register an interest in a broadcast in onResume and unregister in onPause. In this example we will try to send an sms message directly from an activity and then register an interest in the results of a Pending Intent, "SENT_SMS". First we modify the AndroidManifest.xml file to give us permission to send a sms message.
<uses-sdk android:minSdkVersion="3" />
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
We may need import the following:
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
We declare the necessary variables and constants:
private static String SENT_SMS_FLAG= "SENT_SMS";
private static int MAX_SMS_CHAR= 160;
private SmsManager mySMS= SmsManager.getDefault();
Here is our buttonClicked handler where we send a sms message along with a "SENT_SMS" PendingIntent. So we wrap an Intent into a PendingIntent and then send the PendingIntent as a parameter in sendTextMessage.
// SEND SMS BUTTON EVENT HANDLER
final Button buttonSendSMS= (Button)findViewById(R.id.ButtonSMSSendTextMessage);
buttonSendSMS.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String destination= editTextPhoneNumber.getText().toString();
String msg= editTextSMSCipherText.getText().toString();
String smsServiceCenterAddress= null;
// pre conditions
if (msg.length()<1){ // actually IV == 8!
editTextSMSCipherText.setError("Invalid Empty Message");
editTextSMSCipherText.setText("");
editTextSMSCipherText.requestFocus();
return;
}
if (msg.length()>MAX_SMS_CHAR){
editTextSMSCipherText.setError("Error. Message Is Too Large");
editTextSMSCipherText.requestFocus();
}
if (destination.length()<7){
editTextPhoneNumber.setError("Invalid Phone #");
editTextPhoneNumber.setText("");
editTextPhoneNumber.requestFocus();
return;
}
Intent intentSent= new Intent(SENT_SMS_FLAG);
PendingIntent pendingIntentSent= PendingIntent.getBroadcast(SendSMS.this,0,intentSent,0);
try {
mySMS.sendTextMessage(destination,smsServiceCenterAddress,msg,pendingIntentSent,null);
}
catch (IllegalArgumentException e){
editTextPhoneNumber.setError(e.getMessage()); // see pre-conditions
editTextPhoneNumber.setText("");
editTextPhoneNumber.requestFocus();
}
}
});
Note the call to SendSMS.this where SendSMS is the name of this class. This is preferable to calling getBaseContext().
Here is our BroadcastReceiver as an inner class:
// SMS Receiver
private BroadcastReceiver sentReceiver= new BroadcastReceiver() {
@Override
public void onReceive(Context c, Intent i) {
// TODO Auto-generated method stub
switch(getResultCode()) {
case Activity.RESULT_OK:
editTextPhoneNumber.setHint("Success Sending SMS");
editTextPhoneNumber.setText("");
break;
default:
editTextPhoneNumber.setError("Failure Sending SMS");
editTextPhoneNumber.setText("");
break;
}
}
};
Finally, we register an interest in the pending intent in onResume and unregister in onPause:
protected void onResume() {
super.onResume();
registerReceiver(sentReceiver, new IntentFilter(SENT_SMS_FLAG));
}
protected void onPause() {
super.onPause();
unregisterReceiver(sentReceiver);
}
When the message is sent, we trap for the pending intent in onReceive and signal the success in the UI.
Success!
Unfortunately, the message delivered pending intent "DELIVER_SMS" is not currently supported on the Evo.
JAL