阅读所有SMS来自特定发件人发件人、SMS

2023-09-12 08:40:07 作者:白马过平川

我如何读取来自特定发件人的所有短信息给我吗?例如。我要扫描)的主体,以及b)所有的短信的是来自TM-MYAMEX'到手机的日期/时间。

How do I read all SMSes from a particular sender to me? E.g. I want to scan a) the body, and b) the date/time of all SMSes that came from 'TM-MYAMEX' to the phone.

有些网站似乎表明这可以从内容://短信/收件箱来读。我无法弄清楚到底如何。另外不知道这是否是支持大多数手机。我使用的是GALAXY S2。

Some websites seem to indicate this can be read from "content://sms/inbox". I couldn't figure out exactly how. Also not sure if it is supported on most phones. I am using a Galaxy S2.

推荐答案

试试这个方法:

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='123456789'", 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中:

AndroidManifest.xml:

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