如何从Messenger获取短信日期?

时间:2018-08-12 08:07:02

标签: android date time cursor sms

Android默认Messenger使用接收到的时间(图片)将SMS分为几组。
我正在尝试使用光标在自定义列表视图中显示短信收件箱,例如默认Messenger:

    // Create Inbox box URI
    Uri inboxURI = Uri.parse( "content://sms/inbox" );
    // List required columns
    String[] reqCols = new String[]{"_id", "address", "body", "date"};
    // Get Content Resolver object, which will deal with Content
    // Provider
    ContentResolver contentResolver = getContentResolver();
    // Fetch Inbox SMS Message from Built-in Content Provider
    Cursor cursor = contentResolver.query( inboxURI, reqCols, null, null,
            null );


    sms = new ArrayList<>();
    String date = "";
    cursor.moveToLast();
    while (cursor.moveToPrevious() || cursor.isLast()) {
        //to get phone number
        address = cursor.getString( cursor.getColumnIndex( "address" ) );
        //to get massage
        body = cursor.getString( cursor.getColumnIndexOrThrow( "body" ) );
        //to get date of sms
        date = cursor.getString( cursor.getColumnIndexOrThrow( "date" ) );
        Long timestamp = Long.parseLong( date );
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis( timestamp );
        Date finalDate = calendar.getTime();
        smsDate = finalDate.toString();

但是它不能那样工作。就像“ Sun Aug 12 07:13:13 .....”。我想显示与默认Messenger相同的时间。解决办法是什么?

enter image description here

1 个答案:

答案 0 :(得分:0)

在Sms表中,日期以毫秒为单位存储为INTEGER。因此,使用

 long sms_time= cur.getLong(cur.getColumnIndexOrThrow("date")) on Cursor.

这是接收SMS的毫秒时间。现在以毫秒为单位获取当前时间。

 long now = System.currentTimeMillis();//get Current Time

现在以分钟计算M =差异:

long minutes=(now - sms_time)/(1000*60);

来源:https://stackoverflow.com/a/13138710/3155082