我正在制作一个应用程序,它根据发件人号码在收件箱中搜索邮件。这是我的代码:
public void onClick(View v)
{
Toast.makeText(this, "search", Toast.LENGTH_LONG).show();
final Uri SMS_INBOX = Uri.parse("content://sms/inbox");
String[] colList = {"address", "body"};
Cursor c = getContentResolver().query(SMS_INBOX, colList, null, null,"DATE desc");
String yz= String.valueOf(c.getCount());
Toast.makeText(this, yz, Toast.LENGTH_LONG).show();
if(c.moveToFirst())
{
Toast.makeText(this, searchtxt.getText(), Toast.LENGTH_LONG).show();
for(int i=0;i<c.getCount();i++)
{
String number=c.getString(c.getColumnIndexOrThrow("address")).toString();
boolean match = number.toUpperCase().indexOf(searchtxt.getText().toString()) != -1;
if (match)
{
Toast.makeText(this, String.valueOf(i), Toast.LENGTH_LONG).show();
String body= c.getString(c.getColumnIndexOrThrow("body")).toString();
tv.setText(tv.getText() + "\n" + body);
}
c.moveToNext();
}
}
else
{
Toast.makeText(this, "Inside else", Toast.LENGTH_LONG).show();
}
c.close();
}
上面的代码工作正常,但它检索收件箱中的所有消息。我希望它只检索与发件人号码匹配的那些消息。为此,我尝试使用 LIKE 子句进行查询,但它返回 0 条记录。使用 LIKE 子句的正确方法是什么。这是我尝试使用 LIKE 的代码
String[] colList = {"address", "body"};
String[] argList = {"'%"+searchtxt.getText().toString()+"%'"};
Cursor c = getContentResolver().query(SMS_INBOX, colList, "address LIKE ?", argList,"DATE desc");
String yz= String.valueOf(c.getCount());
Toast.makeText(this, yz, Toast.LENGTH_LONG).show();
请帮帮我……谢谢……