How to delete sim card contact in android - android

below is my code for delete contact from phone
Uri contactUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(phone));
Cursor cur = mContext.getContentResolver().query(contactUri, null,
null, null, null);
boolean flag = false;
try {
if (cur.moveToFirst()) {
do {
if (cur.getString(
cur.getColumnIndex(PhoneLookup.DISPLAY_NAME))
.equalsIgnoreCase(name)) {
String lookupKey = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
Uri uri = Uri.withAppendedPath(
ContactsContract.Contacts.CONTENT_LOOKUP_URI,
lookupKey);
mContext.getContentResolver().delete(uri, null, null);
flag=true;
break;
}
} while (cur.moveToNext());
}
} catch (Exception e) {
flag=false;
System.out.println(e.getStackTrace());
}
delete contact from phone is working fine but sim contact delete temporary mean when my phone is restart my contact is recover that i deleted.
help in find solution for this problem.
Thanks...

The URI you want to use is this one : content://icc/adn/
Moreover, you have to use the name and the number to delete a contact.
Try something like this (works for me) :
Uri simUri = Uri.parse("content://icc/adn/");
ContentResolver mContentResolver = this.getContentResolver();
Cursor c = mContentResolver.query(simUri, null, null, null, null);
if (c.moveToFirst())
{
do
{
if (/* your condition here */)
{
mContentResolver.delete(
simUri,
"tag='" + c.getString(c.getColumnIndex("name")) +
"' AND " +
"number='" + c.getString(c.getColumnIndex("number")) + "'"
, null);
break;
}
}
while (c.moveToNext());
}
Off course, don't forget these permissions :
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />

Related

Android - Trouble deleting only contacts with a specified note

I am working on a project that creates random contacts but I am having trouble implementing the deletion capability. I have marked each generated contact with a note through
ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID,
rawContactInsertIndex)
.withValue(Data.MIMETYPE, Note.CONTENT_ITEM_TYPE)
.withValue(Note.NOTE, note)
.build());
Now to delete the contacts i need to use something similar to what i have found here
Uri contactUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(phoneNumber));
Cursor cur = ctx.getContentResolver().query(contactUri, null, null,
null, null);
try {
if (cur.moveToFirst()) {
do {
String lookupKey =
cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
Uri uri = Uri.withAppendedPath(
ContactsContract.Contacts.CONTENT_LOOKUP_URI,
lookupKey);
ctx.getContentResolver().delete(uri, null, null);
} while (cur.moveToNext());
}
} catch (Exception e) {
System.out.println(e.getStackTrace());
}
return false;
}
I believe the only difference between the code above and what I am searching for is that the one above uses a phone number to delete the contact but for my purposes I need to delete by the Note field of the contact.
Fixed my problem..I am too new to java and tried comparing strings using '==' instead of stringObject.equals().
here is a little function to delete contacts based on their string in case anyone needs something like that.
public void deleteContacts(String contactNote){
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
String note;
while (cur.moveToNext()) {
note = "";
try{
String lookupKey = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
String noteWhere = ContactsContract.Contacts.LOOKUP_KEY + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] noteWhereParams = new String[]{lookupKey,
ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE};
Cursor noteCur = cr.query(ContactsContract.Data.CONTENT_URI, null, noteWhere, noteWhereParams, null);
if (noteCur.moveToFirst()) {
note = noteCur.getString(noteCur.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE));
if(note.equals(contactNote)) {
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
cr.delete(uri, null, null);
}
}
noteCur.close();
}
catch(Exception e)
{
System.out.println(e.getStackTrace());
}
}
cur.close();
}

How to delete contacts from sim in android

I did following code for deleting selected contacts from sim card. but its not deleting and also not throwing any error.
protected void DeleteContacts(ArrayList<String> ids){
int flg = 0;
String[] strids = new String[ids.size()];
strids = ids.toArray(strids);
for (int i = 0; i < strids.length; i++) {
Cursor sims = getActivity().getContentResolver().query(
Uri.parse("content://icc/adn"), null,
"_id=?", new String[]{strids[i]}, null);
sims.moveToFirst();
if (sims.getCount()>0) {
String phoneNumber = sims.getString(sims.getColumnIndex("number"));
boolean val = deleteContact(phoneNumber);
if (!val)
flg=1;
}
if (flg == 0)
Toast.makeText(getActivity(), "Contact Deleted", Toast.LENGTH_SHORT).show();
sims.close();
}
}
public boolean deleteContact(String phone) {
Cursor cur = getActivity().getContentResolver().query(Uri.parse("content://icc/adn"), null, "number=?", new String[] { phone }, null);
try {
if (cur.moveToFirst()) {
do {
String lookupKey = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
getActivity().getContentResolver().delete(uri, null, null);
return true;
} while (cur.moveToNext());
}
} catch (Exception e) {
System.out.println(e.getStackTrace());
}
return false;
}
You are deleting the contacts from the sqlite database and not the SIM card. For deleting contacts from the SIM card, you need to delete on Uri.parse("content://icc/adn") uri only. But for deletion you need to provide both name and number. Name column has to specified as tag. Check for delete method here https://android.googlesource.com/platform/frameworks/opt/telephony/+/9ebea45a36838f0547a9c30f7cd9c60b03aab3b4/src/java/com/android/internal/telephony/IccProvider.java
Based on my answer here, here is the solution :
Uri simUri = Uri.parse("content://icc/adn/");
ContentResolver mContentResolver = this.getContentResolver();
Cursor c = mContentResolver.query(simUri, null, null, null, null);
if (c.moveToFirst())
{
do
{
if (/* your condition here */)
{
mContentResolver.delete(
simUri,
"tag='" + c.getString(c.getColumnIndex("name")) +
"' AND " +
"number='" + c.getString(c.getColumnIndex("number")) + "'"
, null);
break;
}
}
while (c.moveToNext());
}
Off course, don't forget these permissions :
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />

Checkable Contact List in Android

I want to make a checkable Contact List that should be stored by the application, allow the users to place checks on some contacts, and store users' preferences.
I want to know whether a preference activity can be used to list all contacts as checkboxes,
or whether a custom listview can allow me to save the users preferences for the app?
you can get contact data with following code:
Cursor cursor = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null,
null, null);
cursor.moveToFirst();
if (cursor.getCount() > 0) {
do {
try {
contactId = cursor
.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
Uri contactUri = ContentUris.withAppendedId(
Contacts.CONTENT_URI,
Long.parseLong(contactId));
Uri dataUri = Uri.withAppendedPath(contactUri,
Contacts.Data.CONTENT_DIRECTORY);
Cursor phones = getContentResolver()
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + contactId,
null, null);
if (phones.getCount() > 0) {
try {
Cursor nameCursor = getContentResolver()
.query(dataUri,
null,
Data.MIMETYPE + "=?",
new String[] { StructuredName.CONTENT_ITEM_TYPE },
null);
nameCursor.moveToFirst();
do {
String firstName = nameCursor
.getString(nameCursor
.getColumnIndex(Data.DATA2));
String displayname = cursor
.getString(cursor
.getColumnIndex(Contacts.DISPLAY_NAME_ALTERNATIVE));
lastName = nameCursor
.getString(nameCursor
.getColumnIndex(Data.DATA3));
} while (nameCursor.moveToNext());
nameCursor.close();
} catch (Exception e) {
}
}
phones.close();
}
catch (Exception t) {
}
} while (cursor.moveToNext());
}
after get list of your contact name or anything that you want you need create custom adapter to show this data, for creating the custom list see this link and this.
//////////////////////
EDIT:
you can get phone number with following code:
String number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

Deleting all Contacts programmatically

The delete all contacts method that seems to do it's job is:
ContentResolver contentResolver = myActivity.getContentResolver();
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext()) {
String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
contentResolver.delete(uri, null, null);
}
Is there a way to adjust this to delete RawContacts instead of contacts? Something like this:
ContentResolver contentResolver = myActivity.getContentResolver();
Cursor cursor = contentResolver.query(ContactsContract.RawContacts.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext()) {
contentResolver.delete(ContactsContract.RawContacts.CONTENT_URI, null, null);
}
I'm looking for other ways to delete all contacts. What's the best way to clear all the contact tables (Contacts, Raw Contacts, Data)?
Inside the for loop you call below method,
This is all we need.
To delete Contact with phone number and name given
public static boolean deleteContact(Context ctx, String phone, String name) {
Uri contactUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phone));
Cursor cur = ctx.getContentResolver().query(contactUri, null, null, null, null);
try {
if (cur.moveToFirst()) {
do {
if (cur.getString(cur.getColumnIndex(PhoneLookup.DISPLAY_NAME)).equalsIgnoreCase(name)) {
String lookupKey = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
ctx.getContentResolver().delete(uri, null, null);
return true;
}
} while (cur.moveToNext());
}
} catch (Exception e) {
System.out.println(e.getStackTrace());
}
return false;
}
And remind to add read/write contact permission
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />

Android Contacts provider get only phone contacts with all emails

I need to get all phone contacts and their email address and photo uri:
This is what am doing:
private void getContacts() {
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(Contacts.CONTENT_URI, null, null, null, Contacts.DISPLAY_NAME);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
// if
// (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)))
// > 0) {
Contact contact = new Contact();
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
Uri uri = getContactPhotoUri(Long.parseLong(id));
// set photoUri
contact.setContactPhotoUri(uri);
// set name
contact.setContactName(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
// get the phone number
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
while (pCur.moveToNext()) {
// set phone munber
contact.setContactNumber(pCur.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
contacts.add(contact);
}
pCur.close();
// get email and type
Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[] { id }, null);
while (emailCur.moveToNext()) {
// This would allow you get several email addresses
// if the email addresses were stored in an array
// set email
contact.setContactEmail(emailCur.getString(emailCur
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)));
contacts.add(contact);
}
emailCur.close();
}
}
cur.close();
contactAdapter = new ContactAdapter(this, R.id.contactList, contacts);
// }
}
public Uri getContactPhotoUri(long contactId) {
Uri photoUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
photoUri = Uri.withAppendedPath(photoUri, Contacts.Photo.CONTENT_DIRECTORY);
return photoUri;
}
My problem am getting all contacts including gmail contacts, i dont want gmail contacts to be included. And the time taken is also very slow. How do i optimize this, I know its taking time coz i am using many cursors.. but dont know how to make a single cusror that can give me name email number photo uri ...Thanks!
UPDATED FINAL:
private void getContacts() {
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(Data.CONTENT_URI, new String[] { Data.CONTACT_ID, Data.MIMETYPE, Email.ADDRESS,
Contacts.DISPLAY_NAME, Phone.NUMBER }, null, null, Contacts.DISPLAY_NAME);
Contact contact;
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(Data.CONTACT_ID));
String mimeType = cur.getString(cur.getColumnIndex(Data.MIMETYPE));
if (allContacts.containsKey(id)) {
// update contact
contact = allContacts.get(id);
} else {
contact = new Contact();
allContacts.put(id, contact);
// set photoUri
contact.setContactPhotoUri(getContactPhotoUri(Long.parseLong(id)));
}
if (mimeType.equals(StructuredName.CONTENT_ITEM_TYPE))
// set name
contact.setContactName(cur.getString(cur.getColumnIndex(Contacts.DISPLAY_NAME)));
if (mimeType.equals(Phone.CONTENT_ITEM_TYPE))
// set phone munber
contact.setContactNumber(cur.getString(cur.getColumnIndex(Phone.NUMBER)));
if (mimeType.equals(Email.CONTENT_ITEM_TYPE))
// set email
contact.setContactEmail(cur.getString(cur.getColumnIndex(Email.ADDRESS)));
}
}
cur.close();
// get contacts from hashmap
contacts.clear();
contacts.addAll(allContacts.values());
// remove null contacts
for (Contact _contact : contacts) {
if (_contact.getContactName() == null && _contact.getContactNumber() == null
&& _contact.getContactEmail() == null) {
contacts.remove(_contact);
break;
}
}
contactAdapter = new ContactAdapter(this, R.id.contactList, contacts);
contactAdapter.notifyDataSetChanged();
}
public Uri getContactPhotoUri(long contactId) {
Uri photoUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
photoUri = Uri.withAppendedPath(photoUri, Contacts.Photo.CONTENT_DIRECTORY);
return photoUri;
}
You should be able to get all the information needed in one query on Data.CONTENT_URI,
Check out "android.provider.ContactsContract.Data" table and the examples on how to query different types of data Email,Phone,Photo etc...
http://developer.android.com/reference/android/provider/ContactsContract.Data.html
For example:
Cursor data = cntx.getContentResolver().query(Data.CONTENT_URI, new String[] {Data._ID,Data.MIMETYPE,Email.ADDRESS,Photo.PHOTO},Data.CONTACT_ID + "=?" + " AND " + "(" + Data.MIMETYPE + "='" + Photo.CONTENT_ITEM_TYPE + "' OR " + Data.MIMETYPE + "='" + Email.CONTENT_ITEM_TYPE +"')",
new String[] {String.valueOf(contactId)}, null);
Should bring you all the information you need regarding one specific contactId, you could theoretically ask for all contacts and sort the information yourself.
As for filtering gmail contacts this is a more complex issue, take a look at ACCOUNT_NAME / TYPE http://developer.android.com/reference/android/provider/ContactsContract.RawContacts.html
parameter and a discussion regarding this issue here:
What is the default Account Type / Name for contacts on Android Contact Application?

Categories

Resources