问题描述
下面是我的代码,用于手机
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()); }
从手机删除联系人正常工作正常,但SIM联系人删除临时意味着我的手机重新启动我的联系人是恢复我删除的. 帮助找到此问题的解决方案. 谢谢...
推荐答案
要使用的URI是这个:content://icc/adn/
而且,您必须使用name和number删除联系人.尝试这样的东西(适用于我):
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()); }
<uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.WRITE_CONTACTS" />
问题描述
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" />