0

I am doing an app which when received a SMS from a specific number and if that text message has been opened will update the system that the message has been read .So that if someone opens the message using a third party app, it will be marked as read. Is there a way I can achieve this. What I want is that system should update the server that the message has been read.

`

import android.app.Notification;'
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import android.util.Log;
import android.widget.Toast;
import android.content.*;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;

import android.telephony.gsm.SmsMessage;

public class SMSReciever extends BroadcastReceiver { 
    private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
    private static final String TAG = "smsfwd";
    private static final String LOG_TAG = "SMSReceiver";
    public static int MSG_TPE=0;

       public void onCreate(){
            Log.i("APP","SMS Receiver started.");
       }



    @Override
    public void onReceive(Context context, Intent intent) { 
        String number = null;
        String body = null;
        Log.i(LOG_TAG, "onReceive");
        Log.i(TAG, "Intent recieved: " + intent.getAction());

        if (intent.getAction() == SMS_RECEIVED) {
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                Object[] pdus = (Object[])bundle.get("pdus");
                final SmsMessage[] messages = new SmsMessage[pdus.length];
                for (int i = 0; i < pdus.length; i++) {
                    messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                }
                if (messages.length > -1) {
                    Log.i(TAG, "Message recieved: " + messages[0].getMessageBody());
                    //NetComm.SendMessage("me", messages[0].getOriginatingAddress(), messages[0].getMessageBody());
                    Uri uri = Uri.parse("content://sms/inbox");
                    Log.i(TAG, "Intent recieved: " );

                    Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
                    try{

                    while (cursor.moveToNext()) {
                            if ((cursor.getString(cursor.getColumnIndex("address")).equals(number)) && (cursor.getInt(cursor.getColumnIndex("read")) == 0)) {
                                if (cursor.getString(cursor.getColumnIndex("body")).startsWith(body)) {
                                    String SmsMessageId = cursor.getString(cursor.getColumnIndex("_id"));
                                    ContentValues values = new ContentValues();
                                    values.put("read", true);
                                    context.getContentResolver().update(Uri.parse("content://sms/inbox"), values, "_id=" + SmsMessageId, null);
                                    return;
                                }
                            }
                        }
              }catch(Exception e)
              {
                  Log.e("Mark Read", "Error in Read: "+e.toString());
              }
                }

            }
        }}
        `  
Tech Guy
  • 1
  • 2
  • there are uri corresponding to messages, you probably can register to it – njzk2 Sep 25 '12 at 16:33
  • Could you tell me how I can implement it?I have come across couple of documents but cannot understand them clearly. – Tech Guy Sep 25 '12 at 16:36
  • Check these two questions: http://stackoverflow.com/questions/8637271/android-how-to-mark-sms-as-read-in-onreceive But instead of changing the read field just read it. http://stackoverflow.com/questions/6059604/set-sms-as-read-in-android – Paul Sep 25 '12 at 16:37

0 Answers0