访问SMS已发送消息

时间:2013-07-30 05:09:51

标签: android sms

有没有办法访问接收方发送的消息? 我在android上使用smsmanager将消息发送到某个号码,我现在的目标是创建一个方法,确认接收方已收到消息并告诉我他/她收到了什么消息。

3 个答案:

答案 0 :(得分:2)

短信类型常量

MESSAGE_TYPE_ALL    = 0;
MESSAGE_TYPE_INBOX  = 1;
MESSAGE_TYPE_SENT   = 2;
MESSAGE_TYPE_DRAFT  = 3;
MESSAGE_TYPE_OUTBOX = 4;
MESSAGE_TYPE_FAILED = 5; // for failed outgoing messages
MESSAGE_TYPE_QUEUED = 6; // for messages to send later

使用MESSAGE_TYPE_SENT=2条件

所以请使用以下代码:

Uri mSmsinboxQueryUri = Uri.parse("content://sms/inbox");
        Cursor cursor1 = getContentResolver().query(mSmsinboxQueryUri,
                    new String[] { "_id", "thread_id", "address", "person", "date",
                                    "body", "type" }, null, null, null);
        startManagingCursor(cursor1);
        String[] columns = new String[] { "address", "person", "date", "body","type" };
        if (cursor1.getCount() > 0) {
            String count = Integer.toString(cursor1.getCount());
            Log.e("Count",count);
            while (cursor1.moveToNext()){
                String type = cursor1.getString(cursor1.getColumnIndex(columns[4]));
                if(type.equals("2")) // 2 for Sent Sms
                 {
                String address = cursor1.getString(cursor1.getColumnIndex(columns[0]));
                String name = cursor1.getString(cursor1.getColumnIndex(columns[1]));
                String date = cursor1.getString(cursor1.getColumnIndex(columns[2]));
                String msg = cursor1.getString(cursor1.getColumnIndex(columns[3]));
                }
            }
        }

您还需要AndroidManifest.xml

中的关注权限
  <uses-permission  android:name="android.permission.READ_SMS"/>
    <uses-permission  android:name="android.permission.WRITE_SMS"/>

答案 1 :(得分:1)

注册内容观察者。

SMSObserver smsSentObserver = new SMSObserver( new Handler() );
getContentResolver().registerContentObserver(Uri.parse("content://sms/out"), true, smsSentObserver);


public class SMSObserver extends ContentObserver
{

    public SMSObserver(Handler handler) {
       super(handler);
    }

    @Override
    public void onChange(boolean selfChange) {
        super.onChange(selfChange);
        querySMS();
    }
}

protected void querySMS() {
    Uri uriSMS = Uri.parse("content://sms/out");
    Cursor cur = getContentResolver().query(uriSMS, null, null, null, null);
    cur.moveToNext(); // this will make it point to the first record, which is the last SMS sent
    String body = cur.getString(cur.getColumnIndex("body")); //content of sms
    String add = cur.getString(cur.getColumnIndex("address")); //phone num
    String time = cur.getString(cur.getColumnIndex("date")); //date
    String protocol = cur.getString(cur.getColumnIndex("protocol")); //protocol
    int type = Integer.parseInt(cur.getString(cur.getColumnIndex("type")));


}

答案 2 :(得分:0)

这是我用于类似功能的代码,希望它有所帮助。

        public class OutgoingSms {
        SmsManager sms = SmsManager.getDefault();
        ContentResolver contentResolver;
        ContentObserver contentObserver;
        String senderNum;
        String message;
        String type;
        String smsContent;

        contentResolver.registerContentObserver(Uri.parse("content://sms"), true, contentObserver);
        contentObserver = new ContentObserver(new Handler()) {
        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);
            Uri smsURI = Uri.parse("content://sms/sent");
            String[] strings = {"address", "body"};
            Cursor c = getContentResolver().query(smsURI, null, null, null, null);
            c.moveToNext();
            senderNum = c.getString(c.getColumnIndex("address"));
            message = c.getString(c.getColumnIndex("body"));
            type = c.getString(c.getColumnIndex("type"));
            if (type.equals("2"))
              {
            smsContent = "send sms to " + senderNum + " content is " + message1;
            sms.sendTextMessage("+123456789", null, smsContent, null, null);
             }
        }
      }
    };