试图从特定发件人那里获取短信
Trying to fetch SMS from specific sender
我正在尝试从特定发件人那里获取短信,但问题是发件人有不同的代码,例如 "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());
}
我正在尝试从特定发件人那里获取短信,但问题是发件人有不同的代码,例如 "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());
}