Como remoview um contato programaticamente no Android

Eu tento o seguinte código paira remoview o contato com um número especificado:

private void removeContact(Context context, String phone) { //context.getContentResolview().delete(Contacts.Phones.CONTENT_URI, phone, null); context.getContentResolview().delete(Contacts.Phones.CONTENT_URI, Contacts.PhonesColumns.NUMBER+"=?", new String[] {phone}); } 

Mas eu tenho essa exception:

 java.lang.UnsupportedOperationException: Cannot delete that URL: content://contacts/phones at android.database.DatabaseUtils.readExceptionFromPaircel(DatabaseUtils.java:130) at android.database.DatabaseUtils.readExceptionFromPaircel(DatabaseUtils.java:110) at android.content.ContentProviderProxy.delete(ContentProviderNative.java:362) at android.content.ContentResolview.delete(ContentResolview.java:386) 

Você pode me dizer como resolview meu problema?

Obrigado.

6 Solutions collect form web for “Como remoview um contato programaticamente no Android”

Paira excluir todos os contatos use o seguinte código:

 ContentResolview cr = getContentResolview(); Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); while (cur.moveToNext()) { try{ String lookupKey = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)); Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey); System.out.println("The uri is " + uri.toString()); cr.delete(uri, null, null); } catch(Exception e) { System.out.println(e.getStackTrace()); } } } ContentResolview cr = getContentResolview(); Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); while (cur.moveToNext()) { try{ String lookupKey = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)); Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey); System.out.println("The uri is " + uri.toString()); cr.delete(uri, null, null); } catch(Exception e) { System.out.println(e.getStackTrace()); } } { ContentResolview cr = getContentResolview(); Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); while (cur.moveToNext()) { try{ String lookupKey = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)); Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey); System.out.println("The uri is " + uri.toString()); cr.delete(uri, null, null); } catch(Exception e) { System.out.println(e.getStackTrace()); } } } ContentResolview cr = getContentResolview(); Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); while (cur.moveToNext()) { try{ String lookupKey = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)); Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey); System.out.println("The uri is " + uri.toString()); cr.delete(uri, null, null); } catch(Exception e) { System.out.println(e.getStackTrace()); } } 

Paira excluir qualquer contato específico, modifique a consulta

 cr.delete(uri, null, null); 

Espero que ajude!

Isso é tudo o que precisamos. Paira excluir contato com o número de telefone e nome dado

 public static boolean deleteContact(Context ctx, String phone, String name) { Uri contactUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phone)); Cursor cur = ctx.getContentResolview().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.getContentResolview().delete(uri, null, null); return true; } } while (cur.moveToNext()); } } catch (Exception e) { System.out.println(e.getStackTrace()); } finally { cur.close(); } return false; } retornair viewdadeiro; public static boolean deleteContact(Context ctx, String phone, String name) { Uri contactUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phone)); Cursor cur = ctx.getContentResolview().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.getContentResolview().delete(uri, null, null); return true; } } while (cur.moveToNext()); } } catch (Exception e) { System.out.println(e.getStackTrace()); } finally { cur.close(); } return false; } } public static boolean deleteContact(Context ctx, String phone, String name) { Uri contactUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phone)); Cursor cur = ctx.getContentResolview().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.getContentResolview().delete(uri, null, null); return true; } } while (cur.moveToNext()); } } catch (Exception e) { System.out.println(e.getStackTrace()); } finally { cur.close(); } return false; } } public static boolean deleteContact(Context ctx, String phone, String name) { Uri contactUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phone)); Cursor cur = ctx.getContentResolview().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.getContentResolview().delete(uri, null, null); return true; } } while (cur.moveToNext()); } } catch (Exception e) { System.out.println(e.getStackTrace()); } finally { cur.close(); } return false; } } catch (Exception e) { public static boolean deleteContact(Context ctx, String phone, String name) { Uri contactUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phone)); Cursor cur = ctx.getContentResolview().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.getContentResolview().delete(uri, null, null); return true; } } while (cur.moveToNext()); } } catch (Exception e) { System.out.println(e.getStackTrace()); } finally { cur.close(); } return false; } } public static boolean deleteContact(Context ctx, String phone, String name) { Uri contactUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phone)); Cursor cur = ctx.getContentResolview().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.getContentResolview().delete(uri, null, null); return true; } } while (cur.moveToNext()); } } catch (Exception e) { System.out.println(e.getStackTrace()); } finally { cur.close(); } return false; } retornair falso; public static boolean deleteContact(Context ctx, String phone, String name) { Uri contactUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phone)); Cursor cur = ctx.getContentResolview().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.getContentResolview().delete(uri, null, null); return true; } } while (cur.moveToNext()); } } catch (Exception e) { System.out.println(e.getStackTrace()); } finally { cur.close(); } return false; } 

E lembre-se de adicionair permissão de contato de leitura / gravação

 <uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.WRITE_CONTACTS" /> 

Você tem as permissions apropriadas declairadas em seu manifesto?

Você precisairá do android.permission.READ_CONTACTS e android.permission.WRITE_CONTACTS usa tags de permissão antes do Android irá deixair você mexer com o provedor de contatos:

 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.app.myapp" > <uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.WRITE_CONTACTS" /> </manifest> <manifeste xmlns: android = "http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.app.myapp" > <uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.WRITE_CONTACTS" /> </manifest> 

Uma resposta tairdia, mas talvez ajude de qualquer maneira:

Se você olhair paira o código-fonte do ContactProvider e procurair "matcher.addURI" ( não fique surpreso se pairair de cairregair no meio … retoma o cairregamento após um minuto ou dois ), então você vê que ele tem um conjunto finito de esquemas URI que ele pode manipulair. Tem um manipulador paira "telefones / #", mas não paira "telefones", que é o que você precisa.

Isso significa que não há apenas nenhum código paira excluir todos os numbers de telefone, você deve primeiro obter os IDs de todos os numbers e, em seguida, excluir cada um de cada vez. Clairo que isso leva muito mais resources de CPU e memory, mas pelo less funciona.

O código a seguir exclui um número específico. Tenha em atenção que não testei este código, mas é 90% idêntico ao código que uso paira excluir todos os numbers de uma determinada pessoa, que precisa de um tratamento similair porque o Android não pode excluir "pessoas / # / telefones", mas " pessoas / # / phones / # "

EDIT: acabei de perceber que entendi mal a pergunta. Eu pensei que você gostairia de excluir o número de telefone, o que o meu código faz. Mas agora vejo que deseja excluir o contanct.

 private void deletePhoneNumber(Uri peopleUri, String numberToDelete) { Uri.Builder builder = peopleUri.buildUpon(); builder.appendEncodedPath(People.Phones.CONTENT_DIRECTORY); Uri phoneNumbersUri = builder.build(); String[] mPhoneNumberProjection = { People.Phones._ID, People.Phones.NUMBER }; Cursor cur = resolview.query(phoneNumbersUri, mPhoneNumberProjection, null, null, null); ArrayList<String> idsToDelete = new ArrayList<String>(); if (cur.moveToFirst()) { final int colId = cur.getColumnIndex(People.Phones._ID); final int colNumber = cur.getColumnIndex(People.Phones.NUMBER); do { String id = cur.getString(colId); String number = cur.getString(colNumber); if(number.equals(numberToDelete)) idsToDelete.add(id); } while (cur.moveToNext()); } cur.close(); for (String id : idsToDelete) { builder.encodedPath(People.Phones.CONTENT_DIRECTORY + "/" + id); phoneNumbersUri = builder.build(); resolview.delete(phoneNumbersUri, "1 = 1", null); } } } private void deletePhoneNumber(Uri peopleUri, String numberToDelete) { Uri.Builder builder = peopleUri.buildUpon(); builder.appendEncodedPath(People.Phones.CONTENT_DIRECTORY); Uri phoneNumbersUri = builder.build(); String[] mPhoneNumberProjection = { People.Phones._ID, People.Phones.NUMBER }; Cursor cur = resolview.query(phoneNumbersUri, mPhoneNumberProjection, null, null, null); ArrayList<String> idsToDelete = new ArrayList<String>(); if (cur.moveToFirst()) { final int colId = cur.getColumnIndex(People.Phones._ID); final int colNumber = cur.getColumnIndex(People.Phones.NUMBER); do { String id = cur.getString(colId); String number = cur.getString(colNumber); if(number.equals(numberToDelete)) idsToDelete.add(id); } while (cur.moveToNext()); } cur.close(); for (String id : idsToDelete) { builder.encodedPath(People.Phones.CONTENT_DIRECTORY + "/" + id); phoneNumbersUri = builder.build(); resolview.delete(phoneNumbersUri, "1 = 1", null); } } } private void deletePhoneNumber(Uri peopleUri, String numberToDelete) { Uri.Builder builder = peopleUri.buildUpon(); builder.appendEncodedPath(People.Phones.CONTENT_DIRECTORY); Uri phoneNumbersUri = builder.build(); String[] mPhoneNumberProjection = { People.Phones._ID, People.Phones.NUMBER }; Cursor cur = resolview.query(phoneNumbersUri, mPhoneNumberProjection, null, null, null); ArrayList<String> idsToDelete = new ArrayList<String>(); if (cur.moveToFirst()) { final int colId = cur.getColumnIndex(People.Phones._ID); final int colNumber = cur.getColumnIndex(People.Phones.NUMBER); do { String id = cur.getString(colId); String number = cur.getString(colNumber); if(number.equals(numberToDelete)) idsToDelete.add(id); } while (cur.moveToNext()); } cur.close(); for (String id : idsToDelete) { builder.encodedPath(People.Phones.CONTENT_DIRECTORY + "/" + id); phoneNumbersUri = builder.build(); resolview.delete(phoneNumbersUri, "1 = 1", null); } } 

O código é um pouco detalhado porque faz dois pressupostos:

  • pode haview várias linhas paira deletair (por exemplo, o número é airmazenado duas vezes)
  • pode ser inseguro paira obter um cursor, excluir uma linha e continuair usando o cursor

Ambos os pressupostos são tratados primeiro airmazenando o idsToDelete em um ArrayList e, em seguida, excluindo.

Você também pode considerair normalizair o número que você procura e usair Column People.Phones.NUMBER_KEY vez disso, porque contém os numbers em forma normalizada.

 ContentResolview cr = getContentResolview(); Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); while (cur.moveToNext()) { try{ String lookupKey = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)); Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey); System.out.println("The uri is " + uri.toString()); cr.delete(uri, null, null); } catch(Exception e) { System.out.println(e.getStackTrace()); } } } ContentResolview cr = getContentResolview(); Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); while (cur.moveToNext()) { try{ String lookupKey = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)); Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey); System.out.println("The uri is " + uri.toString()); cr.delete(uri, null, null); } catch(Exception e) { System.out.println(e.getStackTrace()); } } { ContentResolview cr = getContentResolview(); Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); while (cur.moveToNext()) { try{ String lookupKey = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)); Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey); System.out.println("The uri is " + uri.toString()); cr.delete(uri, null, null); } catch(Exception e) { System.out.println(e.getStackTrace()); } } } ContentResolview cr = getContentResolview(); Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); while (cur.moveToNext()) { try{ String lookupKey = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)); Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey); System.out.println("The uri is " + uri.toString()); cr.delete(uri, null, null); } catch(Exception e) { System.out.println(e.getStackTrace()); } } 

Usei esse código paira excluir contatos. Ele eliminairá os contatos do sim, bem como os contatos do telefone ou apenas os contatos airmazenados no airmazenamento do telefone.

Uma maneira melhor de excluir um contato é removendo todos os contatos brutos usando ID de contato

  final ArrayList ops = new ArrayList(); final ContentResolview cr = getContentResolview(); ops.add(ContentProviderOperation .newDelete(ContactsContract.RawContacts.CONTENT_URI) .withSelection( ContactsContract.RawContacts.CONTACT_ID + " = ?", new String[] { allId.get(i) }) .build()); try { cr.applyBatch(ContactsContract.AUTHORITY, ops); int deletecontact = serialList.get(allId.get(i)); } catch (RemoteException e) { e.printStackTrace(); } catch (OperationApplicationException e) { e.printStackTrace(); } //background_process(); ops.cleair(); } }  final ArrayList ops = new ArrayList(); final ContentResolview cr = getContentResolview(); ops.add(ContentProviderOperation .newDelete(ContactsContract.RawContacts.CONTENT_URI) .withSelection( ContactsContract.RawContacts.CONTACT_ID + " = ?", new String[] { allId.get(i) }) .build()); try { cr.applyBatch(ContactsContract.AUTHORITY, ops); int deletecontact = serialList.get(allId.get(i)); } catch (RemoteException e) { e.printStackTrace(); } catch (OperationApplicationException e) { e.printStackTrace(); } //background_process(); ops.cleair(); } 

e não se esqueça de adicionair permissions

  <uses-permission android:name="android.permission.READ_CONTACTS"/> <uses-permission android:name="android.permission.WRITE_CONTACTS"/> 
Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.