0

我想从收件箱中读取所有短信并检查特定关键字。我开发了此代码。但它没有产生所需的结果。请建议我进行更正。

    public void onClick(View arg0 ){
        Uri uri = Uri.parse("content://sms/inbox");
        ContentResolver content = getContentResolver();
        Cursor c = content.query(uri,new String[] { "_id","address","body","person"}, null, null, null);
        if(c.getCount() > 0)
        {
            while(c.moveToNext())
            {
                colName = colName + c.getString(c.getColumnIndex("body")) + "\n";
                if(colName.contains("hai"))

                    textview1.setText("present");

                else
                    textview1.setText("not present");
            }
        }

    }
    });

}
4

1 回答 1

1

由于您的问题在提供的信息方面相当模糊,这只是一个猜测:

如果您有多个 SMS 并且其中一个包含单词“hai”,如果此 SMS 不是最后一个 SMS,则结果文本将“不存在”,因为您在每次迭代中都使用光标覆盖此 textview 。

因此,例如,如果您在将 textview 设置为“present”后添加返回,如果该单词出现在至少一个 SMS 中,您应该能够看到结果“present”。

if(colName.contains("hai")){
      textview1.setText("present");
      return; 
}else{
      textview1.setText("not present");
}
于 2012-12-02T18:37:06.017 回答