如何阅读最后的短信

时间:2014-11-28 17:20:52

标签: android

使用下面的代码我在TextView“textStareLed”中写了第一条未读消息。如何写最后一条消息? 我尝试使用cursor.move Previous()但是没有用。

protected void AfisareStareLed() {
    TextView view = new TextView(this);
    Uri uriSMSURI = Uri.parse("content://sms/inbox");   
    Cursor cursor = getContentResolver().query(uriSMSURI, null, null, null, null)
    while (cursor.moveToNext()){
        if(cursor.getString(2).equals("+4xxxxx") && cursor.getString(8).equals("0")){//cursor.getString(8)==0 <=> "read=0"
            TextView textStareLed = (TextView) findViewById(R.id.textStareLed);
            textStareLed.setText(cursor.getString(13));
            }
    }
    markMessageRead();//mark all message "read=1"    
}

1 个答案:

答案 0 :(得分:0)

只需添加&#34;日期desc限制1&#34;

String msgData = "";
        Cursor cursor = getContentResolver().query(Uri.parse("content://sms/inbox"), new String[] {"body"}, null ,null, "date desc limit 1"); //if you change the 1 to 2 you´ll get the last 2 sms...
        cursor.moveToFirst();

        do{

            for(int idx=0;idx<cursor.getColumnCount();idx++)
            {
                msgData += cursor.getString(idx);

            }
        }while(cursor.moveToNext());

        textStareLed.setText(msgData);