9

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());
        }
    }
Gangadhar Nimballi
  • 1,534
  • 3
  • 18
  • 46

2 Answers2

10

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

Techfist
  • 4,314
  • 6
  • 22
  • 32
  • Thank you for reply, Can you please tell how to mark my app as Default SMS app ? – Gangadhar Nimballi Sep 23 '14 at 07:28
  • @GangadharNimbally That link tells you what you need to do for your app to be a default. It's not a trivial task, as your app will then be responsible for many things it probably doesn't already handle. – Mike M. Sep 23 '14 at 08:31
  • @Mike M, Is there any sample app to become default SMS app. Please share it I am poor at android – Gangadhar Nimballi Sep 23 '14 at 10:39
  • @GangadharNimbally Not that I know of. The place to start, though, is with that link. Copy the manifest and add the necessary permissions (there's about 5 or 6 needed, IIRC), create at least a temporary main launcher Activity, compile and run it once, and make sure it shows in the list of eligible default SMS apps. That'll be the bare minimum. Then start adding the additional functionalities. Of course, you could always check out the source code for the AOSP SMS app for a full implementation. – Mike M. Sep 23 '14 at 10:46
  • @GangadharNimbally follow the link and code suggested into it, its already mentioned there how to make you app default, read the article carefully. – Techfist Sep 23 '14 at 11:08
  • if jast with defaul app ca filter sms : so How Vault filter and delete sms however its not default app !! – محمد Dec 14 '14 at 10:11
-1

App can delete Sms at Kitkat WITHOUT default sms app granted.

You need only WRITE_SMS permission and AppOpps manipulation.

After that your sms can be deleted after 10 seconds after receiving, notification also will despear.

Peter Zverkov
  • 145
  • 1
  • 6