pervin
pervin

Reputation: 11

ContentObserver in Android

I have successfully monitor incoming SMS and sent it to database for viewing later. I have read about monitor outgoing sms and I don't really understand how it works. Can someone guide me how to code to monitor outgoing sms by using ContentObserver? I will post all my current codes for incoming sms.need guide from where I should start.

smsReceiver.java

package terima.sms.inbox;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import java.net.*;
import java.io.*;

@SuppressWarnings("deprecation")
public class SmsReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();

        Object messages[] = (Object[]) bundle.get("pdus");
        SmsMessage SMS[] = new SmsMessage[messages.length];
        for (int n = 0; n < messages.length; n++) {
            SMS[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
        }

        String member_id = "1";

        inbox(SMS[0].getOriginatingAddress(), SMS[0].getMessageBody(), member_id);
    }

    public static Boolean inbox(String telefon, String message, String member_id){

        String mesej = "";
        for(int i = 0; i < message.length(); i++)
        {
            if(message.charAt(i) == ' ' || message.charAt(i) == '+')
            {
                if(message.charAt(i) == ' ')
                    mesej += "%20";
                else
                    mesej += "%2B";
            }
            else
            {
                mesej += message.charAt(i);
            }
        }

        try
        {
            URL oracle = new URL("http://192.168.1.111/inbox.php?message=" + mesej + "&telefon=" + telefon + "&member=" + member_id);
            BufferedReader in = new BufferedReader(new InputStreamReader(oracle.openStream()));

            String fetch, total = "";
            while ((fetch = in.readLine()) != null)
                total += fetch;
            in.close();

            if(total.equals("1"))
                return true;

            return false;
        }
        catch(Exception e)
        {
            return false;
        }
    }
}

thanks in advance.

Upvotes: 1

Views: 1715

Answers (1)

benjamin
benjamin

Reputation: 118

To catch outgoing SMS you should use a ContentObserver coupled with a ContentResolver. Here is the code I use in the ContentObserver:

public class SmsObserver extends ContentObserver{   

int smsCount;

public SmsObserver(Context context) {
    super(new Handler());

    smsCount = 0;

}


public void onChange(boolean selfChange){   
    super.onChange(selfChange);
  readSms();
}

private void readSms(){
    Uri uriSMS = Uri.parse("content://sms");
    Cursor cur = context.getContentResolver().query(uriSMS, null, null, null, "_id");

    cur.moveToLast();
    int id = Integer.parseInt(cur.getString(cur.getColumnIndex("_id")));

    if(cur != null && id != smsCount && id>0){
        smsCount = id;

    int type = Integer.parseInt(cur.getString(cur.getColumnIndex("type")));

        if(type == 1){
            // handle the received sms

        }
        else{
            // handle the sent sms
        }
    }

    cur.close();

}
}

After that you should create an instance of SmsOberver and register it on "content://sms". It will be triggered more often than when you send or receive a new message, so this is why I have a smsCount field.

Upvotes: 3

Related Questions