0

i want to read all sms from inbox and check for a particular keyword.I developed this code.but it doesn't produce the desired result.please suggest me corrections to be made.

    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

Since your questions is rather vague when it comes to supplied information this is just a guess:

If you have multiple SMS and one of them contains the word "hai", if this SMS is not the last one the resulting text will be "not present" since you are overwriting this textview in every iteration with the cursor.

So for example if you add a return after setting the textview to "present" you should be able to see the result "present" if the word is present in at least one SMS.

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