Hello. I ask for help. I could not figure it out myself. I decided to play around with SMS in android- pull them out, do something to take off, etc. Pull it out - that's all. I don’t understand how to do the sorting, for example, if we press a button to search for a specific text in a message, and then output these messages.

public class MessageBox extends Activity { // GUI Widget Button start; TextView lblMsg, lblNo; ListView lvMsg; String number; // Cursor Adapter SimpleCursorAdapter adapter; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.messagebox); start = (Button)findViewById(R.id.start); lvMsg = (ListView) findViewById(R.id.lvMsg); Uri inboxURI = Uri.parse("content://sms/inbox"); String[] reqCols = new String[]{"_id", "address", "body"}; ContentResolver cr = getContentResolver(); Cursor c = cr.query(inboxURI, reqCols, null, null, null); adapter = new SimpleCursorAdapter(this, R.layout.row, c, new String[]{"body", "address"}, new int[]{ R.id.lblMsg, R.id.lblNumber}); lvMsg.setAdapter(adapter); start.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { } }); } } 

    2 answers 2

    As an option, you can simply

      Cursor c = cr.query(inboxURI, reqCols, "body='%text%", null, null); 

    "body like '%text%" will find all messages where there is the word text and around it any characters. if somewhere you didn’t write me right

    • Thank you very much, but the% signs do not work. If I understand correctly, the correct entry will be such "body = '% text%'" - without% it finds a word and then if it makes up all the SMS. doesn't want to find him in the text - Alerx
    • figured out, you need so "body like '% text%'" - Alerx
    • @Alerx - just keep in mind that this is only a case sensitive search, SQLite has a birth injury - case insensitive search only works for ASCII dialing. - Barmaley
    1. To sort there is an ORDER BY which must be entered in your query last parameter
    2. There is a special undocumented URI for SMS search: content://mms-sms/search - rummage through the sources :)

    Update

    In the source code for the search is this:

     @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { Uri u = Uri.parse(String.format( "content://mms-sms/searchSuggest?pattern=%s", selectionArgs[0])); Cursor c = getContext().getContentResolver().query( u, null, null, null, null); return new SuggestionsCursor(c, selectionArgs[0]); } 
    • is it possible, for example, to substitute sum and count into a query, or it will be necessary to create your own database and from there select, - J Mas
    • SMS database is not available, access is only via ContentProvider , so normal SQL queries are not available - Barmaley
    • by URI: content // mms-sms / search very little information - Alerx
    • @Alerx what you have :) It’s said that this is undocumented everything - Use the Source, Luke - see update - Barmaley