Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android按发件人名称查询短信收件箱_Android_Sms - Fatal编程技术网

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

Android按发件人名称查询短信收件箱,android,sms,Android,Sms,我想通过特定的发件人获取手机中的短信列表。这些短信是从短信网关发送的,所以我没有发件人的号码。然而,我知道发件人的名字,如“谷歌” 这就是我迄今为止所尝试的 final String SMS_URI_INBOX = "content://sms/inbox"; Uri uri = Uri.parse(SMS_URI_INBOX); String[] projection = new String[] { "_id", "address", "person", "body", "date", "t

我想通过特定的发件人获取手机中的短信列表。这些短信是从短信网关发送的,所以我没有发件人的号码。然而,我知道发件人的名字,如“谷歌”

这就是我迄今为止所尝试的

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");
但是,光标为空。

请尝试执行此操作-

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" />

`试试这个,它用于获取从某个地址收到的最后一个mesage。如果要读取所有消息,请迭代此光标

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

        }

    }
`你就快到了

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网关使用名称作为地址。

这不是我想要的。您的答案显示了如何从发件人的电话号码查询短信。但我想从发送者的名字查询它。