是否可以从我的应用程序中删除手机联系人

时间:2013-08-01 06:16:33

标签: android

我从手机中获取了所有联系人,并在列表视图中列出。我能够删除列表视图中的本地联系人以及数据库现在我想删除手机联系人是可能的。为了删除目的,我使用以下代码。

if (position == 1) {
    db.deleteContact(item_position + 1);
    from.remove(item_position);
    note.notifyDataSetChanged();
}

4 个答案:

答案 0 :(得分:1)

获取所选项目名称并将其分配给名称,然后尝试以下代码。

ContentResolver cr = getContentResolver();
                        String where = ContactsContract.Data.DISPLAY_NAME + " = ? ";
                        String[] params = new String[] {name};

                        ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
                        ops.add(ContentProviderOperation.newDelete(ContactsContract.RawContacts.CONTENT_URI)
                                .withSelection(where, params)
                                .build());
                        try {
                            cr.applyBatch(ContactsContract.AUTHORITY, ops);
                        } catch (RemoteException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (OperationApplicationException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                        Toast.makeText(getApplicationContext(), "Deleted the contact with name '" + name +"'", Toast.LENGTH_SHORT).show();
                        from.remove(item_position);
                        note.notifyDataSetChanged();

答案 1 :(得分:0)

如果您需要删除Contact Uri的联系方式

Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);

1.Get all contact from device and store into one container

while (phones.moveToNext())
{
  String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
  String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

}
phones.close();

2.从容器中选择联系人 然后传递选定的值以进行performe删除操作

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

答案 2 :(得分:0)

它可能与从移动设备上删除特定日志相同,但不确定它可以完成多远。 删除特定通话记录的代码如下:

public void DeleteNumFromCallLog(ContentResolver resolver, String strNum) {
    try {
        String strUriCalls = "content://call_log/calls";
        Uri UriCalls = Uri.parse(strUriCalls);
        if (null != resolver) {
            resolver.delete(UriCalls, CallLog.Calls._ID + "=?",
                    new String[] { strNum });
        }
    } catch (Exception e) {
        e.getMessage();
    }
}

答案 3 :(得分:0)

这就是我们所需要的。删除联系人电话号码和姓名

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

并提醒添加读/写联系人权限

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