Listen for incomming SMS

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Listen for incomming SMS

Postby Untouchab1e » Mon Sep 06, 2010 12:17 pm

So, I stumbled across the following code. However, its old and doesnt work with the newest SDK.. Some of the imports doesnt exist anymore and such..

Anyone care to help me out getting this working?

Code: Select all
package org.apache.sms;

import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.IntentReceiver;
import android.os.Bundle;
import android.provider.Telephony;
import android.util.Log;
import android.telephony.gsm.SmsMessage;

public class SMSApp extends IntentReceiver {
    private static final String LOG_TAG = "SMSApp";

    /* package */ static final String ACTION =
            "android.provider.Telephony.SMS_RECEIVED";

    public void onReceiveIntent(Context context, Intent intent) {
        if (intent.getAction().equals(ACTION)) {
            StringBuilder buf = new StringBuilder();
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                SmsMessage[] messages = Telephony.Sms.Intents.getMessagesFromIntent(intent);
                for (int i = 0; i < messages.length; i++) {
                    SmsMessage message = messages[i];
                    buf.append("Received SMS from  ");
                    buf.append(message.getDisplayOriginatingAddress());
                    buf.append(" - ");
                    buf.append(message.getDisplayMessageBody());
                }
            }
            Log.i(LOG_TAG, "[SMSApp] onReceiveIntent: " + buf);
            NotificationManager nm = (NotificationManager) context.getSystemService(
                    Context.NOTIFICATION_SERVICE);

            nm.notifyWithText(123, buf.toString(),
                    NotificationManager.LENGTH_LONG, null);

        }
    }

    private void appendData(StringBuilder buf, String key, String value) {
        buf.append(", ");
        buf.append(key);
        buf.append('=');
        buf.append(value);
    }
}
Untouchab1e
Freshman
Freshman
 
Posts: 8
Joined: Tue Jun 02, 2009 8:24 am

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 5 guests