无法获取地址中包含文本的SMS

时间:2018-10-25 20:44:08

标签: android sms android-contentresolver

我正在尝试获取以TM-,ID-,AX-,AD-开头的SMS,但是我正在获取短信中唯一具有数字的SMS。

我无法收到任何在地址中包含文字的短信。

这是我的代码:

    Uri uriSms = Uri.parse("content://sms/inbox");
String[] projection = new String[]{"_id", "address", "date", "body", "person", "type"};
m_SMSCursor =context.getContentResolver().query(uriSms, projection, null,null,null);   
if(m_SMSCursor.moveToFirst())

{

    // Read each row from cursor and store it into the database
    do {
        // Extract fields from cursor
        final String number = m_SMSCursor.getString(1);
        final long date = m_SMSCursor.getLong(2);
        final String body = m_SMSCursor.getString(3);
        final String person = m_SMSCursor.getString(4);
        final int type = m_SMSCursor.getInt(5);

        try {
            // Insert call log into database
            long l;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    // create object for CallLogAdapter
                    SMSDataAdapter objSmsDataAdapter = new SMSDataAdapter(
                            context);
                    // open the database
                    objSmsDataAdapter = objSmsDataAdapter.Open();
                    try {


                        boolean isVMPresent = number.startsWith("VM-");
                        boolean isLMPresent = number.startsWith("LM-");
                        boolean isTMPresent = number.startsWith("TM-");
                        boolean isVKPresent = number.startsWith("VK-");
                        boolean isIXPresent = number.startsWith("IX-");
                        boolean isADPresent = number.startsWith("AD-");
                        boolean isAXPresent = number.startsWith("AX-");
                        boolean isBWPresent = number.startsWith("BW-");
                        boolean isIDPresent = number.startsWith("ID-");
                        boolean isIMPresent = number.startsWith("IM-");
                        Log.i(TAG, "Number1 : " + number + " body1 : " + body + " peron : " + person + " type : " + type);
                        if (isVMPresent || isLMPresent || isTMPresent || isVKPresent || isIXPresent || isADPresent || isAXPresent || isBWPresent || isIDPresent || isIMPresent) {


                            long lNumberOfEntry = objSmsDataAdapter.Insert(number, body, date, DATA_NOT_ANALYZED);

                        }

                    } catch (SQLException e1) {


                    } finally {
                        // Close the database
                        objSmsDataAdapter.Close();
                    }

                }
            }).start();

        } catch (SQLException e) {

            return FAILURE;

        }
    } while (m_SMSCursor.moveToNext());
}

但是我能够提取所有短信,但地址中有文字的除外。我可以获取地址中包含号码的短信。

请给我提示或参考。

1 个答案:

答案 0 :(得分:0)

使用此方法首先获取列表中的所有数据,然后通过smsList.getNumber()访问该数字

   List<SMSData> smsList = new ArrayList<SMSData>();

   Uri uri = Uri.parse("content://sms/inbox");
   Cursor c= getContentResolver().query(uri, null, null ,null,null);
   startManagingCursor(c);

   // Read the sms data and store it in the list
   if(c.moveToFirst()) {
       for(int i=0; i < c.getCount(); i++) {
           SMSData sms = new SMSData();
           sms.setBody(c.getString(c.getColumnIndexOrThrow("body")).toString());
           sms.setNumber(c.getString(c.getColumnIndexOrThrow("address")).toString());
           smsList.add(sms);

           c.moveToNext();
       }
   }
   c.close();

SMSData:

public class SMSData {

 // Number from witch the sms was send
private String number;
// SMS text body
private String body;

public String getNumber() {
    return number;
}

public void setNumber(String number) {
   this.number = number;
}

public String getBody() {
   return body;
}  

public void setBody(String body) {
   this.body = body;
  }

}