首页 > 解决方案 > How to get the SMS date from messenger?

问题描述

Android default messenger sort SMS in several groups using received time(picture).
I am trying to show sms inbox in custom listview like default messenger using cursor:

    // 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();

but it is not working like that.It's like "Sun Aug 12 07:13:13 .....". I want to show time same as default messenger. What is the solution?

enter image description here

标签: androiddatetimecursorsms

解决方案


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

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

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

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

现在计算 M=Difference In Minutes:

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

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


推荐阅读