Delete SMS from android on 4.4.4 (Affected rows = 0(Zero), after deleted)

Gangadhar Nimballi picture Gangadhar Nimballi · Sep 23, 2014 · Viewed 20.3k times · Source

i want to send SMS from my android device and delete it from mydevice(Sent messages).

SMS are saved in device(4.4.4) but SMS is not deleted with my code. after delete rows affected = 0(Zero).

My device vesrion is 4.4.4.

In other devices, SMS are not being saved. Why does SMS's are saved in Android 4.4.4?

I dont want to save my sent sms's or failure sms's(Which are not sent).

please help me.

My permissions

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

My Code is to send SMS

SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(number, null, msg, null, null);

I am calling the method deleteSMS() from handler with postdelay of 5seconds

Handler handler = new Handler();
handler.postDelayed(new Runnable() {

            @Override
            public void run() {
                String message = CastApplication.mPref.getString(context.getResources().getString(R.string.pref_message_to_friend), "");
                deleteSMS(context, message, number);

                if (MyCastFragment.getInstance() != null) {
                    MyCastFragment.getInstance().updateView();
                }

                progressDialog.dismiss();
                context.finish();
            }
        }, 5000);

Delete SMS

public void deleteSMS(Context ctx, String message, String number) {
        try {
            Uri uriSms = Uri.parse("content://sms");
            Cursor c = ctx.getContentResolver().query(uriSms,
                new String[] { "_id", "thread_id", "address",
                    "person", "date", "body" }, null, null, null);

            Log.i(TAG, "c count......"+c.getCount());
            if (c != null && c.moveToFirst()) {
                do {
                    long id = c.getLong(0);
                    long threadId = c.getLong(1);
                    String address = c.getString(2);
                    String body = c.getString(5);
                    String date = c.getString(3);
                    Log.e("log>>>", "0>" + c.getString(0) + "1>" + c.getString(1)   + "2>" + c.getString(2) + "<-1>"  + c.getString(3) + "4>" + c.getString(4)+ "5>" + c.getString(5));
//                    Log.e("log>>>", "date" + c.getString(0));

//                    if (body.contains(getResources().getText(R.string.invite_text).toString()) && address.equals(number)) {
                    if (message.equals(body) && address.equals(number)) {
                        // mLogger.logInfo("Deleting SMS with id: " + threadId);
                        int rows = ctx.getContentResolver().delete(Uri.parse("content://sms/" + id), "date=?",new String[] { c.getString(4) });
                        Log.e("log>>>", "Delete success......... rows: "+rows);
                        Log.e("log>>>", "Delete success......... body: "+body);
                    }
                } while (c.moveToNext());
            }

        } catch (Exception e) {
            Log.e("log>>>", e.toString());
            Log.e("log>>>", e.getMessage());
        }
    }

Answer

Techfist picture Techfist · Sep 23, 2014

Unless your app is marked as default SMS app in device, you wont be able to play with SMS Provider, please read SMS guidelines for the same for KITKAT