特定のAndroidの連絡先グループをプログラムで削除するには?
私はこれを試しました、
私には機能しません。アイデアや提案があれば教えてください。本当に助かります。
よろしくお願いします!!!
特定のAndroidの連絡先グループをプログラムで削除するには?
私はこれを試しました、
私には機能しません。アイデアや提案があれば教えてください。本当に助かります。
よろしくお願いします!!!
グループを適切に削除する方法を見つけました。削除するグループの ID を適切なクエリで取得する必要があります。その後、この ID と Groups.CONTENT_URI を使用してこのグループを削除できます。
以下に例を投稿します(コードに合わせてください)。
// First get the id of the group you want to remove
long groupId = null;
Cursor cursor = mContext.getContentResolver.query(Groups.CONTENT_URI,
new String[] {
Groups._ID
}, Groups.TITLE + "=?", new String[] {
yourGroupTitle // Put here the name of the group you want to delete
}, null);
if (cursor != null) {
try {
if (cursor.moveToFirst()) {
groupId = cursor.getLong(0);
}
} finally {
cursor.close();
}
}
// Then delete your group
ArrayList<ContentProviderOperation> mOperations = new ArrayList<ContentProviderOperation>();
// Build the uri of your group with its id
Uri uri = ContentUris.withAppendedId(Groups.CONTENT_URI, groupId).buildUpon()
.appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true")
.build();
ContentProviderOperation.Builder builder = ContentProviderOperation.newDelete(uri);
mOperations.add(builder.build());
// Then apply batch
try {
mContext.getContentResolver().applyBatch(ContactsContract.AUTHORITY, mOperations);
} catch (Exception e) {
Log.d("########## Exception :", ""+e.getMessage());
}
それが役立つことを願っています。
まず、特定のグループIDを持つすべての連絡先IDを見つけます。次に、ContentProviderOperation
削除する連絡先ごとにを作成し、最後に削除操作のリストを適用します。
private void deletaAllInGroup(Context context, long groupId)
throws RemoteException, OperationApplicationException{
String where = String.format("%s = ?", GroupMembership.GROUP_ROW_ID);
String[] whereParmas = new String[] {Long.toString(groupId)};
String[] colSelection = new String[] {Data.CONTACT_ID};
Cursor cursor = context.getContentResolver().query(
Data.CONTENT_URI,
colSelection,
where,
whereParmas,
null);
ArrayList<ContentProviderOperation> operations =
new ArrayList<ContentProviderOperation>();
// iterate over all contacts having groupId
// and add them to the list to be deleted
while(cursor.moveToNext()){
String where = String.format("%s = ?", RawContacts.CONTACT_ID);
String[] whereParams = new String[]{Long.toString(cursor.getLong(0))};
operations.add(ContentProviderOperation.newDelete(RawContacts.CONTENT_URI)
.withSelection(where, whereParams)
.build());
}
context.getContentResolver().applyBatch(
ContactsContract.AUTHORITY, operations );
}
このコードを使用してグループを削除しました。しかし、あまり明確に機能していません。
String groupName = "Your Group Name";
try {
ContentResolver cr = this.getContentResolver();
ContentValues groupValues = null;
groupValues = new ContentValues();
groupValues.put(ContactsContract.Groups.GROUP_VISIBLE,0);
cr.update (ContactsContract.Groups.CONTENT_URI, groupValues, ContactsContract.Groups.TITLE+ "=?", new String[]{groupName}) ;
cr.delete(ContactsContract.Groups.CONTENT_URI, ContactsContract.Groups.TITLE+ "=?", new String[]{groupValue});
}
catch(Exception e){
Log.d("########### Exception :",""+e.getMessage());
}
このコードを実行した後。グループが削除されます。私は電話の連絡先または人々と検索グループに行きます。表示されていません。しかし、プログラムですべてのグループをプログラムで読み取ると、削除されたグループが表示されます。