سؤال

Hi stackoverflow I'm trying to develop an application to add SMS to prrogrammatically, I'm using the following code to add SMS

private void addSMS()
{       
    Uri uri = Uri.parse("content://sms/");
    ContentValues cv2 = new ContentValues();
    cv2.put("address", "+91956322222");
    cv2.put("date", "1309632433677");
    cv2.put("read", 1);
    cv2.put("type", 2);
    cv2.put("body", "Hey");
    getContentResolver().insert(uri, cv2);
    cv2.clear();
}

Permissions :

<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.WRITE_SMS"/>

Problem is the Time of the message, it's displaying the time we added the message, but not the date we have passed in the list of messages, but when I open the message we added then the time will be correct as our input, please help me to solve this riddle.

Thanks.

هل كانت مفيدة؟

المحلول

This is a known problem I think. Try to add this line in the end:

getContentResolver().delete(Uri.parse("content://sms/conversations/-1"), null, null);

If you want more explanation you should check this out!

نصائح أخرى

Here is the working code, Thanks to @Amulya Khare

private void addSMS()
{       
    Uri uri = Uri.parse("content://sms/");
    ContentValues cv2 = new ContentValues();
    cv2.put("address", "+91956322222");
    cv2.put("date", "1309632433677");
    cv2.put("read", 1);
    cv2.put("type", 2);
    cv2.put("body", "Hey");
    getContentResolver().insert(uri, cv2);
    /** This is very important line to solve the problem */
    getContentResolver().delete(Uri.parse("content://sms/conversations/-1"), null, null);
    cv2.clear();
}

Please follow the link to get more information Android programatically inserted SMS have incorrect timestamp in Messaging apps

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top