1

我已成功监控传入的短信并将其发送到数据库以供稍后查看。我已阅读有关监控传出短信的信息,但我并不真正了解它是如何工作的。有人可以指导我如何使用 ContentObserver 编写代码来监控传出的短信吗?我将从我应该开始的地方发布我当前的所有传入 sms.need 指南的代码。

短信接收器.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;
        }
    }
}

提前致谢。

4

1 回答 1

3

要捕获传出 SMS,您应该使用 ContentObserver 和 ContentResolver。这是我在 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();

}
}

之后,您应该创建一个 SmsOberver 实例并将其注册到“content://sms”。它会比您发送或接收新消息时更频繁地触发,所以这就是我有一个 smsCount 字段的原因。

于 2012-09-07T21:20:37.243 回答