Search code examples
javaandroidsmssmsmanager

Trying to insert SMS to Inbox , nothing happens


I am trying to insert SMS from a backup but when I ran the code nothing happens no errors or anything and no sms in sms application or inbox.

Also tried add date,read status etc. but didn't work

Have READ and WRITE sms permissions. minSDK: 23

Here is the code:

public void addSms(String number , String body){

    ContentValues values = new ContentValues();
    values.put("address", number);
    values.put("body", body);
    getContentResolver().insert(Uri.parse("content://sms/inbox"),values);

}

and calling addSms from:

for (int i = 0; i < smssJson2.length(); i++) {
            try {
                JSONObject obj = smssJson2.getJSONObject(i);
                String body = obj.getString("message");
                String number = obj.getString("number");
                addSms(number,body);
            } catch (Exception e) {
                e.getLocalizedMessage();
            }
        }

checking inbox with(this code is working):

Uri uriSMSURI = Uri.parse("content://sms/inbox");
    Cursor cur = getContentResolver().query(uriSMSURI, null, null, null, null);

    while (cur != null && cur.moveToNext()) {
        String address = cur.getString(cur.getColumnIndex("address"));
        String body = cur.getString(cur.getColumnIndexOrThrow("body"));

        JSONObject obj = new JSONObject();
        try {
            obj.put("number",address);
            obj.put("message",body);
            Log.d("SMS",obj.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    cur.close();

Solution

  • Solved it!

    if app's android version 4.4(KitKat) or higher you have to make your app default messaging app for add sms to SmsProvider or send or receive sms and mms.

    and for a backup/restore app you should follow these steps:

    1- Change your app as default messaging app.

    2- Insert smsses

    3- Change default messaging app with previously one.

    Resource and for more information HERE!