7

我可以从收件箱中读取消息:-

 Uri uriSMSURI = Uri.parse("content://sms/inbox");

 Cursor cur = getContentResolver().query(uriSMSURI, null, null, null, null);

我从此访问日期:-

   date = cur.getString(cur.getColumnIndexOrThrow("date"));

但现在的问题是它给出了当前时间而不是收件箱中的消息时间。抱歉编辑不好,任何想法都将不胜感激。提前致谢!

4

3 回答 3

13

使用此代码:

ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.query( Uri.parse( "content://sms/inbox" ), null, null, null, null);
cursor.moveToFirst();
String date =  cursor.getString(cursor.getColumnIndex("date"));
Long timestamp = Long.parseLong(date);    
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timestamp);
Date finaldate = calendar.getTime();
String smsDate = finaldate.toString();
Log.d(Home.class.getName(), smsDate);
于 2012-10-30T12:10:07.220 回答
7

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

millis = cur.getLong(cur.getColumnIndexOrThrow("date"))在光标上。

然后,使用

DateFormat.format("EEEE, MMMM dd, yyyy h:mm:ss aa", new Date(millis))

得到一个很好的可读字符串。

于 2012-10-30T12:12:00.327 回答
3
    /**
 * Read messages of respective phone number from msg 
 * 
 * @param activity
 * @param ph_number
 * @return
 */

public static void getMessageDetails(Activity activity, String ph_number) {


    String forDelete_thread_id = "";
    String forDelete_id = "";
    String address = ph_number;
    Uri allMsg = Uri.parse("content://sms/");
    Cursor managedCursor = activity.getContentResolver().query(
            allMsg,
            new String[] { "_id", "thread_id", "address", "date", "body",
                    "type" }, "address=?", new String[] { address }, null);

    while (managedCursor.moveToNext()) {

        String msg_id = managedCursor.getString(0);
        String thread_id = managedCursor.getString(1);
        String address_b = managedCursor.getString(2);
        String date = managedCursor.getString(3);
        String msg_body = managedCursor.getString(4);
        String type = managedCursor.getString(5);


    }
}
于 2014-06-22T06:29:21.133 回答