2013-08-01 105 views
1

我從手機中獲取所有聯繫人並在列表視圖中列出。我能夠刪除我的列表視圖中的本地聯繫人以及數據庫現在我想刪除手機聯繫人那可能。爲了移除目的,我使用下面的代碼。是否有可能從我的應用程序中刪除手機聯繫人

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

回答

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(); 
+0

感謝您的回答,它工作正常。 – Aravin

0

,如果你需要從聯繫烏里

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.Select從container`聯繫人刪除聯繫人 然後通過選擇價值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; 
} 
+0

對不起我不能夠讓你的代碼,它是puzzle.Can你簡單 – Aravin

+0

好的解釋。首先你的所有聯繫,並存儲到container..now.you要刪除特定的接觸,從而選擇接觸從容器,並通過這個信息最後的代碼,我寫了deleteContact()mehtod ..你收到嗎? – QuokMoon

+0

當我選擇聯繫人應該刪除的列表視圖項目時,我的聯繫人列表視圖中。 – Aravin

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(); 
    } 
} 
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" /> 
+0

Godra我想用我的列表查看項目刪除聯繫方式如何做到這一點。 – Aravin

+0

從列表和手機(設備)以及? –

相關問題