如何在listview中一起显示发送和接收的消息

时间:2017-10-18 19:32:51

标签: android

public void refreshSmsInbox(){         ContentResolver contentResolver = getContentResolver();         Cursor smsInboxCursor = contentResolver.query(Uri.parse(“content:// sms / inbox”),null,null,null,null);         int indexBody = smsInboxCursor.getColumnIndex(“body”);         int indexAddress = smsInboxCursor.getColumnIndex(“address”);         if(indexBody< 0 ||!smsInboxCursor.moveToFirst())return;

    arrayAdapter.clear();
    do {
        String str = "SMS From: " + smsInboxCursor.getString(indexAddress) +
                "\n" + smsInboxCursor.getString(indexBody) + "\n";
        smsMessagesList.add(str);
    } while (smsInboxCursor.moveToNext());

}

1 个答案:

答案 0 :(得分:0)

public void refreshSmsInbox(){         ContentResolver contentResolver = getContentResolver();         Cursor smsInboxCursor = contentResolver.query(Uri.parse(" content:// sms /"),null,null,null,null);

    if (smsInboxCursor.getColumnIndex("body") < 0 || !smsInboxCursor.moveToFirst()) return;
    do {
        String contactName=smsInboxCursor.getString(smsInboxCursor.getColumnIndex("address"));
        String mess=smsInboxCursor.getString(smsInboxCursor.getColumnIndex("body"));

        long timeMillis = smsInboxCursor.getColumnIndex("date");
        Date date = new Date(timeMillis);
        SimpleDateFormat format = new SimpleDateFormat("MM/DD/YY");
        String dateText = format.format(date);

        String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(timeMillis),
                TimeUnit.MILLISECONDS.toMinutes(timeMillis) % TimeUnit.HOURS.toMinutes(1),
                TimeUnit.MILLISECONDS.toSeconds(timeMillis) % TimeUnit.MINUTES.toSeconds(1));

        MessageList message=new MessageList(contactName,mess,dateText,hms,R.drawable.receive);
        smsMessagesList.add(message);
    } while (smsInboxCursor.moveToNext());
}