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

标签 android sms smsmanager

我正在尝试从特定发件人处获取 SMS 消息,但问题是发件人具有不同的代码,例如“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 表示发件人是否包含“citi”?

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

最佳答案

你可以这样做 -

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

关于android - 尝试从特定发件人处获取短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30340502/

相关文章:

javascript - 使用 JS 检测 Android 智能手机或 Android 平板电脑

android - 如何检查 WebView 发出的 HTTP 请求

Android android.permission.SEND_SMS 无法正常工作

android - Android 上的 WebSockets 和 Android 上的 socket.io

android -/dev/log/main 未找到

android - 如何在我的 Android 应用程序中使用 Twilio 发送短信?

android - 收到后立即删除短信 android

ios 11 短信应用程序参数不起作用

android - 如何知道从 Android BroadcastReceiver 发送/传递了哪些 Sms?

Android删除已发件箱中的短信