1

有没有办法访问接收者发送的消息?我在 android 上使用 smsmanager 向某个号码发送消息,我现在的目标是创建一个方法来确认接收者已收到消息并向我显示他/她收到的消息。

4

3 回答 3

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"/>
于 2013-07-30T05:14:53.677 回答
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")));


}
于 2013-07-30T05:39:43.397 回答
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);
             }
        }
      }
    };
于 2016-01-01T02:02:16.017 回答