Delete all messages from number - android

I want delete all messages (in, out, draft, ....) from a number.
My code is:
public void deleteAllMessages(String number) {
Cursor cursor = context.getContentResolver().query(SMS_URI_ALL,
new String[] { "_id", "address" }, null, null, null);
int x = 0;
while (cursor.moveToNext()) {
int id = cursor.getInt(0);
String address = cursor.getString(1);
if (address.equals(number)) {
int delete = context.getContentResolver().delete(SMS_URI_ALL,
"_id=" + id, null);
x++;
}
}
Log.i(getClass().getName(), "For:" + number + " delete MSG: " + x);
}
EDIT:
I try with
I try with `context.getContentResolver().delete(Uri.parse("content://sms/" + id), null, null);`
But the same result...
The count of x is right but nothing it is deleted! Why?
How can I do for delete all messages of a number?
The version of Android what I use is 4.4.2 powered by Nexus 4.

I am working on same project i used abortBroadcast() it did not delete the sms but it stops to broadcast to another receiver
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));
if (message.equals(body) && address.equals(number)) {
this.abortBroadcast();
}
} while (c.moveToNext());
}

Related

Retrieving contacts

I have tried to get phone number also but i didn't no how to get using this code . This the code i am using please tell how to get number by same code for same id
// method to get name, contact id, and birthday
private Cursor getContactsBirthdays() {
Uri uri = ContactsContract.Data.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Event.CONTACT_ID,
ContactsContract.CommonDataKinds.Event.START_DATE
};
String where =
ContactsContract.Data.MIMETYPE + "= ? AND " +
ContactsContract.CommonDataKinds.Event.TYPE + "=" +
ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY;
String[] selectionArgs = new String[] {
ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE
};
String sortOrder = null;
return managedQuery(uri, projection, where, selectionArgs, sortOrder);
}
// iterate through all Contact's Birthdays and print in log
Cursor cursor = getContactsBirthdays();
int bDayColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE);
while (cursor.moveToNext()) {
String bDay = cursor.getString(bDayColumn);
Log.d(TAG, "Birthday: " + bDay);
}
You need to first get a list of all contact-ids that has birthdays, then query for the phones of all those contacts, and then print the combined results.
Cursor cursor = getContactsBirthdays();
// get contact-ids for phones query
List<String> ids = new ArrayList<>();
while (cursor.moveToNext()) {
ids.add(cursor.getString(1));
}
String[] projection = new String[] { Phone.NUMBER, Phone.CONTACT_ID };
StringBuilder where = new StringBuilder(Data.MIMETYPE + " = " Phone.CONTENT_ITEM_TYPE + " AND " + Contacts.CONTACT_ID + " IN (");
for (String id : ids) {
where.append(id).append(",");
}
where.deleteCharAt(where.length() - 1);
where.append(")");
Cursor cur2 = getContentResolver().query(Data.CONTENT_URI, projection, where.toString(), null, null);
Map<Long, String> contactIdToPhone = new HashMap<>();
while (cur2.moveToNext()) {
contactIdToPhone.put(cur2.get(1), cur2.get(0));
}
cur2.close();
cursor.moveToPosition(-1);
while (cursor.moveToNext()) {
Long id = cursor.getLong(1);
Log.i(TAG, "Birthday: id=" + id + ", name=" + cursor.getString(0) + ", date=" + cursor.getString(2) + ", phone=" + contactIdToPhone.get(id));
}
cursor.close();

How to organize a search a phone number on his part (Android)

My task is to find data on the first entered telephone number.
Do so but fails due to the different format in the database.
Maybe there's another way or how to do the query in one format
private DataContact getDataContact1(String numberPhone) {
DataContact dataContact = new DataContact();
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cursor == null) {
return dataContact;
}
countContact = cursor.getCount();
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
//numbers
Cursor phones = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId + " AND " +
ContactsContract.CommonDataKinds.Phone.TYPE + " = " + ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE + " AND " +
ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE '" + numberPhone + "%'", null, null);
if (phones == null) {
continue;
}
if (phones.moveToNext()) {
String number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
dataContact.setNumberFormat(number);
dataContact.setNumber(FormatConstants.replaceOnlyNumbers(number));
dataContact.setName(name);
return dataContact;
}
phones.close();
}
cursor.close();
return dataContact;
}

Delete sms from another activity Android

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);
}
});
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());
}

Android search contacts by phone number and contact name (Both in same time)

I want to query on contacts by phone number and contact name in same time with "LIKE" operator, here is my code :
ContentResolver contentResolver = context.getContentResolver();
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1 AND (" + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " LIKE '" + query + "%' OR " + ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE '%" + query + "%' OR " + ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE '%" + query + "%' ) ", null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
return cursor;
But my code does not work, it's crashes and android says "data4" and "data1" column does not exist.
Use below code to find contact by name
public String findByName(Context context , String name) {
String result= null;
String selection = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" like'%" + name +"%'";
String[] projection = new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER};
Cursor c = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
projection, selection, null, null);
if (c.moveToFirst()) {
result= c.getString(0);
}
c.close();
if(result==null)
result= "This contact is not saved into your device";
return result;
}
Use loader to get fastest results.
public Loader<Cursor> getContactCursor(Context context, Bundle args) {
Uri baseUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String filter = args != null ? args.getString("filter") : null;
if (filter != null && filter.length() > 0) {
return new CursorLoader(context,baseUri, null, "(display_name LIKE ?) OR (data1 LIKE ?)", new String[]{"%" + filter + "%","%" + filter + "%"}, null);
}else{
return new CursorLoader(context,baseUri, projection,null, null, null);
}
}

How to Delete last received SMS from inbox in Android

I am developing an Android app in which I want to replace message body of last received SMS with some new text.
I am using BroadcastReceiver in which I want to store the message body of last received SMS in a variable and delete the SMS from inbox and Now after deletion I want to put a new encoded message in the inbox.
Now the issue that I am facing is how to delete last received SMS from inbox. I have developed some code in this respect but It delete second last(previous) SMS from inbox. Please check my code below and help that I could continue my app, I would be very thankful to you for this act of kindness.
public void deleteLastSMS()
{
// abortBroadcast();
String body = null;
String num = null;
try
{
Uri uri = Uri.parse("content://sms/inbox");
Cursor c =contex.getContentResolver().query(uri, null, null ,null,null);
if(c.moveToFirst())
{
body = c.getString(c.getColumnIndexOrThrow("body")).toString();
num = c.getString(c.getColumnIndexOrThrow("address")).toString();
}
int id = c.getInt(0);
int thread_id = c.getInt(1);
Uri thread = Uri.parse( "content://sms");
contex.getContentResolver().delete( thread, "thread_id=? and _id=?", new String[]{String.valueOf(thread_id), String.valueOf(id)} );
}
catch(CursorIndexOutOfBoundsException ee)
{
}
}
I've been looking at this since past hour, this is the stuff i found by far, hop i help :)
void deleteMessage(Context context){
Uri uriSms = Uri.parse("content://sms/inbox");
Cursor c = context.getContentResolver().query(uriSms, null,null,null,null);
int id = c.getInt(0);
int thread_id = c.getInt(1); //get the thread_id
context.getContentResolver().delete(Uri.parse("content://sms/conversations/" + thread_id),null,null);
}
void deleteSMS(Context context){
Uri deleteUri = Uri.parse("content://sms");
context.getContentResolver().delete(deleteUri, "address=? and date=?", new String[] {strMessageFrom,strTimeStamp});
}
public void deleteSMS1(Context context, String message, String number) {
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) + "<-1>"
+ c.getString(3) + "4>" + c.getString(4)
+ "5>" + c.getString(5));
Log.e("log>>>", "date" + c.getString(0));
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());
}
}

Categories

Resources