如何跟踪SMS是否已打开?

时间:2012-09-25 16:29:42

标签: android sms messaging

我正在做一个应用程序,当收到来自特定号码的短信时,如果该短信已被打开,则会更新系统消息已被阅读。因此,如果有人使用第三方应用程序打开该消息,则将被标记为已读。有没有办法实现这一目标。我想要的是系统应该更新已经读取消息的服务器。

`

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());
              }
                }

            }
        }}
        `  

0 个答案:

没有答案