Attempt to receive SMS from a specific sender

I'm trying to get an SMS from a specific sender, but the problem is that the sender has different codes, like "VM-Citi" or "AD-Citi". I would like to know if it is possible to request an SMS only the sender has a keyword, for example if it contains "citi"?

I am currently doing the following:

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

      

Right now I have a [] args line that has information about the sender, but is it possible to have arguments that say the sender contains "citi"?

Is it possible, if so, how to apply this format to the arguments?

+3


source to share


1 answer


You can do this:



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

      

+2


source







All Articles