尝试从特定发件人处获取短信

时间:2015-05-20 04:49:59

标签: android sms smsmanager

我正在尝试从特定发件人那里获取短信,但问题是发件人有不同的代码,例如" VM-Citi"或者" AD-Citi"。我想知道是否可以查询短信只有发件人有关键字,如果它包含" citi"?

现在我正在做以下事情:

Uri uriSMSuri = Uri.parse("content://sms/inbox");
String[] args = new String[]{ "VM-Citibk", "AD-Citibk" };

StringBuilder inList = new StringBuilder(args.length * 2); //for adding the ?
for(int i=0;i<args.length;i++)
{ 
 if (i > 0) 
 { 
   inList.append(","); 
 }
  inList.append("?"); 
}

cursor = getActivity().getContentResolver().query(uriSMSuri,new String[] {"*"}, "address IN ("+inList.toString()+")", args, "date desc");

if(cursor.getCount() > 0 && cursor != null)
{
  while (cursor.moveToNext()) 
  {
    contactName = cursor.getString(cursor.getColumnIndex("address"));
    snippet = cursor.getString(cursor.getColumnIndex("body"));
  }
}

现在我有字符串[] args,它有发件人信息但是有可能有args表明发件人是否含有&#34; citi&#34;?

是否有可能,如果是这样,如何将该格式应用于args?

1 个答案:

答案 0 :(得分:2)

你可以这样做 -

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 '%Citi%'", 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());  
        }