Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert SMS programmatically on Android 4.4.2

Tags:

android

sms

I have the following code which works fine in older versions of Android, but not on 4.4.2. No errors occur and nothing in logcat, but the messages do not show up in the default SMS app in the emulator. So my question is why it doesn't work on 4.4.2 and what I have to do to make it work again.

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();
  }
like image 530
ardevd Avatar asked Jan 27 '14 09:01

ardevd


1 Answers

Starting from Android 4.4 Kitkat, it is not possible to perform any kind of SMS related operations without being the default SMS app. Restore and Backup apps cannot do it either. Most of them have updated their app to support this functionality and now ask the user to select them as the default SMS app.

like image 143
Saket Avatar answered Oct 15 '22 02:10

Saket