Android API provides a very easy to way to get applications notified for phone related events such as SMS received and SMS sent. In this blog post we will learn how our app can receive these two notifications.
Getting notified for Received SMS
BroadcastReceiver class allows applications to get notified for any android defined filter they want. All you have to do is to extend your class with BroadcastReceiver class and overrides its onReceive method. This method is called when Intent broadcast is received by this BroadcastReceiver. It has two parametes, first is a Context in which the receiver is running , second is the Intent which is being received. Its prototype in BroadcastReceiver class is like this
public abstract void onReceive(Context context,Intent intent);So lets write our simple class whose onReceive method will be called on broadcast for any filter we are interested (in our case it is android.provider.Telephony.SMS_RECEIVED ).
public class MyReceivedSmsReceiver extends BroadcastReceiver {Now sms variable contains our sent SMS.
private static final String TAG="com.usmani.android.MyReceivedSmsReceiver";
@Override
public void onReceive(Context context,Intent intent){
//if you have registered your class for multiple filters you can check
//for which filter it is called like this
if (intent.getAction().CompareTo("android.provider.Telephony.SMS_RECEIVED")==0){
Bundle pudsBundle = intent.getExtras();
Object[] pdus = (Object[]) pudsBundle.get("pdus");
SmsMessage sms =SmsMessage.createFromPdu((byte[]) pdus[0]);
Log.i(TAG, sms.getMessageBody());
}
}
}
Registering your BroadcastReceiver
In order to make our BroadcastReceiver available, we have to register it with android system. There are two methods to do this
1) Describe your receiver in manifest file
2) Dynamically register it using code.
Lets check both the methods
1) Describing in manifest file
<receiver android:name="MyReceivedSmsReceiver" >2) Registering using code
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
BroadcastReceiver receiver=new MyReceivedSmsReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(receiver, filter);
Getting notified for Sent SMS
We cannot use BroadcastReceiver to get notified for Sent SMS. But don't worry we have an easy by using android's ContentObserver class. We register ContentObserver for our interested URI (in case of sms we will use "content://sms"). Whenever content is changed for which we have registered ContentObserver , its onChange method is called. onChange has two overloads
1) void onChange(boolean selfChange);To receive onChange event we will create our class that extends ContentObserver and implement the above mentioned two methods. But for simplicity in this post we will implement the first overload only
2) void onChange(boolean selfChange,Uri uri);
public class MySentSmsHandler extends ContentObserver {
private Context mContext;
public MySentSmsHandler(Context context){
mContext=context;
}
public void onChange(boolean selfChange){Registering your ContentObserver
Cursor cursor = context.getContentResolver().query(
Uri.parse(CONTENT_SMS), null, null, null, null);
if (cursor.moveToNext()) {
String protocol = cursor.getString(cursor.getColumnIndex("protocol"));
int type = cursor.getInt(cursor.getColumnIndex("type"));
// Only processing outgoing sms event & only when it
// is sent successfully (available in SENT box).
if (protocol != null || type != MESSAGE_TYPE_SENT) {
return;
}
int dateColumn = cursor.getColumnIndex("date");
int bodyColumn = cursor.getColumnIndex("body");
int addressColumn = cursor.getColumnIndex("address");
String from = "0";
String to = cursor.getString(addressColumn);
Date now = new Date(cursor.getLong(dateColumn));
String message = cursor.getString(bodyColumn); }
}
}
We can register our ContentObserver like this
ContentObserver observer=new MySentSmsHandler(context);
context.getContentResolver().registerContentObserver(
Uri.parse("content://sms"), true, observer);
Great blog! Is your theme custom made or did you download it from somewhere?
ReplyDeleteA design like yours with a few simple tweeks would really make my blog shine.
Please let me know where you got your theme. Cheers