【问题标题】:How to read all SMS from inbox by more than one sender but not by all sender in android?如何从一个以上的发件人而不是android中的所有发件人读取收件箱中的所有短信?
【发布时间】:2017-10-26 17:05:20
【问题描述】:

我有一个数组列表,其中有一些联系电话,我想将该数组列表传递给地址。这样它就会从arraylist中的选定数字中获取所有消息。 提前谢谢你。

  StringBuilder smsBuilder = new StringBuilder();
    final String SMS_URI_INBOX = "content://sms/inbox";
    final String SMS_URI_ALL = "content://sms/";

    Uri uri = Uri.parse(SMS_URI_INBOX);
    String[] projection = new String[] { "_id", "address", "person", "body", "date", "type" };

    Cursor cur = getContentResolver().query(uri, projection, "address='"+list+"'" , 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);


            String str = "SMS From: " + cur.getString(index_Address) +
                    "\n" + cur.getString(index_Body) + "\n";
            arrayAdapter.add(str);

            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
}

【问题讨论】:

  • 您的代码中的 ArrayList 数字在哪里?
  • 不,我问的是如何将多个联系电话传递给地址? @pleft
  • for len(arrayOfNumbers)=N ... selection = createArraywithStringNTimes("address=?", N).join(" OR ")selectionArgs = arrayOfNumbers .... 所以 selection 将是 "address=? OR address=? OR address=? .... address=?"selectionArgs = new String[] { "N1", "N2", "N3", ... "NN"}
  • 但如果您想要更多地址,则必须创建选择,例如“地址 = ?或地址 = ?”然后 arr_name 应该包含与“?”相同的元素在选择中......对于 2 个元素:“地址 = ?或地址 = ?”对于 3:“地址 = ?或地址 = ?或地址 = ?”等等……

标签: java android sms message telephonymanager


【解决方案1】:
       int argcount = MOBILENUMBER.size(); // number of IN arguments i.e More than one Mobile number for which you want read SMS

    //  String[] args = new String[]{ 1, 2 };
    StringBuilder inList = new StringBuilder(argcount * list.length);
    for (int i = 0; i < argcount; i++)
    {
        if(i > 0)
        {
            inList.append(",");
        }
        inList.append("?");//This will generate Number of "? " to match our item in array list
    }
    Cursor cur = getContentResolver().query(uri, projection, "address IN (" + inList.toString() + ")" , list, "date DESC ");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多