2

I access all sms using ("content://sms/inbox") in my custom list view currently i am getting address body and _id now i want to delete selected sms from another activity please guide me i am beginner in andorid this is my Mainactivity but i want to delete seleted sms from another activity

 Uri uri = Uri.parse("content://sms/");
    ContentResolver contentResolver = getContentResolver();
    Cursor cursor = contentResolver.query(uri, null, null, null, null, null);

    if(cursor !=null  && cursor.moveToFirst()){
        do{
        //    name = getContactName(address);
             tid=             cursor.getString(cursor.getColumnIndexOrThrow("_id"));
            address = cursor.getString(cursor.getColumnIndexOrThrow("address"));
             body = cursor.getString(cursor.getColumnIndexOrThrow("body"));
          if(name==null) {

                list.add(new mybean("" + address, "" + body,""+tid));

            }

            else{
                list.add(new mybean("" + name, "" + body,""+tid));
            }
            my =new  myadapter(this,list);
            lv.setAdapter(my);
        }while(cursor.moveToNext());

    }



        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
                Intent intent =new Intent(MainActivity.this,Main2Activity.class);

                intent.putExtra("delete",list.get(pos).getDel());
                intent.putExtra("sms",list.get(pos).getNumber());
                intent.putExtra("smsmsg",list.get(pos).getMsg());


                startActivity(intent);
            }
        });

1 Answers1

1

Here is the quide to how to delete sms Deleting Android SMS programmatically For kitkat https://android-developers.googleblog.com/2013/10/getting-your-sms-apps-ready-for-kitkat.html First you should choose your app as default sms app then you can delete or remove sms from there.. You can also refer to this post How to delete an SMS from the inbox in Android programmatically? here is the tutorial for deleting sms programmatically http://wisdomitsol.com/blog/android/sms/programmatically-delete-sms-in-android i hope you find these post helpful if any problem you can comment here.

1.First Add permission in manifest 2. write the method

public boolean deleteSms(String smsId) {
    boolean isSmsDeleted = false;
    try {
        mActivity.getContentResolver().delete(
                Uri.parse("content://sms/" + smsId), null, null);
        isSmsDeleted = true;

    } catch (Exception ex) {
        isSmsDeleted = false;
    }
    return isSmsDeleted;
}

you can now delete sms byIds

You can also try this code

try {
    Uri uriSms = Uri.parse("content://sms/inbox");
    Cursor c = context.getContentResolver().query(
            uriSms,
            new String[] { "_id", "thread_id", "address", "person",
                    "date", "body" }, "read=0", null, null);

    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) + "3--->"
                            + c.getString(3) + "4----->" + c.getString(4)
                            + "5---->" + c.getString(5));
            Log.e("log>>>", "date" + c.getString(0));

            ContentValues values = new ContentValues();
            values.put("read", true);
            getContentResolver().update(Uri.parse("content://sms/"),
                    values, "_id=" + id, null);

            if (message.equals(body) && address.equals(number)) {
                // mLogger.logInfo("Deleting SMS with id: " + threadId);
                context.getContentResolver().delete(
                        Uri.parse("content://sms/" + id), "date=?",
                        new String[] { c.getString(4) });
                Log.e("log>>>", "Delete success.........");
            }
        } while (c.moveToNext());
    }
} catch (Exception e) {
    Log.e("log>>>", e.toString());
}
Sardar Khan
  • 845
  • 6
  • 16