Message Listener

Android Development


Recently there was a requirement from customer (major bank) to a create messaging application that can listen for inbound and outbound messages, for the Android-powered BlackBerry PRIV device. I wanted to share my experience of how I implemented the solution by making a sample application. It is useful for developers, if they need to perform some action when a message is sent or received (block it, log it, etc.). At the end, I have provided the GitHub links for downloading the code, for you to extend and use for your own needs.


When the sample application is installed on the device, any SMS you receive or send is reflected by toast. A service class is used which runs at back-end and listens to the messages. A Content observer is registered in the onStart() method of the service, to listen for the changes in the content of the messages.

public intonStartCommand(Intent intent, int flags, intstartId) {
Toast.makeText(this, "Service started", Toast.LENGTH_LONG).show();
myObserver = new MyObserver(new Handler());
ContentResolvercontentResolver = this.getApplicationContext().getContentResolver();
        contentResolver.registerContentObserver(Uri.parse("content://sms"), true, myObserver);

Whenever any change occurs it calls its own overridden method onChange().

class MyObserver extends ContentObserver {
   public MyObserver(Handler handler) {

   public void onChange(booleanselfChange) {
Toast.makeText(getApplicationContext(), "Message listened", Toast.LENGTH_SHORT).show();

For incoming messages, I have also registered a Broadcast receiver which will provide additional information regarding the sender, number and message body.

<action android:name="android.provider.Telephony.SMS_RECEIVED" />

If at some point you want to stop listening messages, you will have to unregister the content observer inside the onDestroy() method

public void onDestroy() {
Toast.makeText(this, "MyService Completed or Stopped.", Toast.LENGTH_SHORT).show();

Our sample application even supports Android permissions starting from the Marshmallow release (API Level 23).

Security for the application is provided by the BID framework. By using the BID framework in our application, we can ensure that the application will only run when the device is in a secure state. Currently this feature is supported only on BlackBerry devices running the Android operating system.

message_listener_1 message_listener_2

Support for Enterprises:

The application can also be leveraged in enterprise environments, as it is also supported when used with Android for Work and Samsung Knox frameworks.

For more information about the installation and usage of Android for Work, feel free to refer to: Developing Apps for Work

GitHub source code:

The source code of the application is available here.

Code for the application developed in BID framework can be found here.

Join the conversation

Show comments Hide comments
+ -