Android按发件人姓名查询短信收件箱

时间:2015-05-11 04:24:42

标签: android sms

我想获取特定发件人手机中的短信列表。这些短信是从短信网关发送的,因此我没有发送者号码。但是,我知道发件人的姓名,例如" Google"。

这是我到目前为止所尝试的

final String SMS_URI_INBOX = "content://sms/inbox";
Uri uri = Uri.parse(SMS_URI_INBOX);
String[] projection = new String[] { "_id", "address", "person", "body", "date", "type" };
Cursor cur = getContentResolver().query(uri, projection, "person LIKE'%" + "Google" + "'", null, "date asc");

但是,光标为空。

3 个答案:

答案 0 :(得分:7)

你快到了

final String SMS_URI_INBOX = "content://sms/inbox";
Uri uri = Uri.parse(SMS_URI_INBOX);
String[] projection = new String[] { "_id", "address", "person", "body", "date", "type" };
Cursor cur = getContentResolver().query(uri, projection, "address LIKE '%Google%'", null, "date asc");

不是查询“人”,而是查询“地址”。 SMS网关使用名称作为地址。

答案 1 :(得分:4)

尝试这样做 -

StringBuilder smsBuilder = new StringBuilder();
       final String SMS_URI_INBOX = "content://sms/inbox"; 
        final String SMS_URI_ALL = "content://sms/";  
        try {  
            Uri uri = Uri.parse(SMS_URI_INBOX);  
            String[] projection = new String[] { "_id", "address", "person", "body", "date", "type" };  
            Cursor cur = getContentResolver().query(uri, projection, "address LIKE '%Google%'", null, "date desc");
             if (cur.moveToFirst()) {  
                int index_Address = cur.getColumnIndex("address");  
                int index_Person = cur.getColumnIndex("person");  
                int index_Body = cur.getColumnIndex("body");  
                int index_Date = cur.getColumnIndex("date");  
                int index_Type = cur.getColumnIndex("type");         
                do {  
                    String strAddress = cur.getString(index_Address);  
                    int intPerson = cur.getInt(index_Person);  
                    String strbody = cur.getString(index_Body);  
                    long longDate = cur.getLong(index_Date);  
                    int int_Type = cur.getInt(index_Type);  

                    smsBuilder.append("[ ");  
                    smsBuilder.append(strAddress + ", ");  
                    smsBuilder.append(intPerson + ", ");  
                    smsBuilder.append(strbody + ", ");  
                    smsBuilder.append(longDate + ", ");  
                    smsBuilder.append(int_Type);  
                    smsBuilder.append(" ]\n\n");  
                } while (cur.moveToNext());  

                if (!cur.isClosed()) {  
                    cur.close();  
                    cur = null;  
                }  
            } else {  
                smsBuilder.append("no result!");  
            } // end if  
            }
        } catch (SQLiteException ex) {  
            Log.d("SQLiteException", ex.getMessage());  
        }  

的AndroidManifest.xml:

<uses-permission android:name="android.permission.READ_SMS" />

答案 2 :(得分:0)

`试试这个用于获取从地址收到的最后一个消息。如果你想阅读所有的消息迭代这个光标。

 Uri mSmsinboxQueryUri = Uri.parse("content://sms/inbox");
 Cursor cursor1 = getContentResolver().query(mSmsinboxQueryUri,
            new String[] { "body", "address" }, null, null, null);

 if (cursor1.getCount() > 0) {
        cursor1.moveToFirst();
        String address = cursor1.getString(1);
        if (address.contains("Google")) {

            String body = cursor1.getString(0);

        }

    }

`