Recipe 5.4 Monitoring Outgoing SMS Messages

Android Versions
Level 1 and above
Permissions
android.permission.READ_SMS
Source Code to Download from Wrox.com
DetectOutgoingSMS.zip

You want your application to monitor all the outgoing SMS messages sent by the user using the built-in Messaging application.

Solution

If you want to monitor user-sent SMS messages through the built-in Messaging application, you need to look out for changes to the SMS content provider used by the Messaging app. All messages received and sent by the Messaging app are stored in the content provider located at content://sms. Hence, if a user sends a message, that message will be stored in this content provider. To monitor for changes in this content provider, you need to use two classes: ContentResolver and ContentObserver:

package net.learn2develop.detectoutgoingsms;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.ContentObserver;
import android.os.Bundle;

public class MainActivity extends Activity {
    ContentResolver contentResolver;
    ContentObserver smsContentObserver;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

}

A ContentResolver object enables you to access a content provider, while a ContentObserver object enables you to register a callback when there is a change to the content of a content provider.

To instantiate the ContentResolver object, ...

Get Android Application Development Cookbook: 93 Recipes for Building Winning Apps now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.