Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
213 views
in Technique[技术] by (71.8m points)

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

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());
        }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...