2016-05-22 11 views
0

連絡先を削除するには、lookup_keyを使用して読んだことが最良の方法です。連絡先が編集されている場合でも、各連絡先には一意のルックアップキーがあります。たとえば、名前や番号などで削除する場合は、複数のエントリを削除できます。連絡先のlookup_keyを使用して連絡先を削除する方法

言って、私は私の電話帳またはSIM内の連絡先のLOOKUP_KEY持っている場合:

String thelookupkey = 1393i2f.3789r504-2927292F4D4D2F35274949374B.2537i1272844629.1728i108 

をどのように私は私の電話帳から、この連絡先を削除することができます。私は私が(はい、MEを書いたと言って誇りに思ってい

public void deletecontactbutton(View view) { 
    context.getContentResolver().delete(ContactsContract.Contacts.CONTENT_LOOKUP_URI, 
      null, thelookupkey); 

} 
+0

はこれをチェックします。http://のstackoverflowを。 com/questions/527216/how-to-remove-a-contact-programmatically-in-android – Rafal

+0

Rafalに感謝します。私はいくつかの答えが危険に紛れているのを発見し、私の電話帳でそれを危険にさらすことは確実ではありませんでした。私は自分の携帯電話で自分のアプリをテストします。私は実際に答えの1つ後にコメントを残しました。 – CHarris

+0

@Rafal私の答えを参照してください。 – CHarris

答えて

0

(また、実験しながら、おそらく私の電話帳を台無しにしたくない)、それは以下のようなものだ知っているが、正確な構文がわかりません! !!)私が欲しいまさに行いサブルーチン:つまり、LOOKUP_KEY値を使用して連絡先(電話、番号、その連絡先に関連付けられているすべての詳細を)削除:

public void deletecontactbutton(View view) { 

     String thelookupkey; 
     thelookupkey = "1885r1471-29373D3D572943292D4333"; 

//  in our cursor query let's focus on the LOOKUP_KEY column 
//  this will give us all the strings in that column 
     String [] PROJECTION = new String [] { ContactsContract.Contacts.LOOKUP_KEY }; 

//  we're going to query all the LOOKUP_KEY strings ; that is, the unique ids of all our contacts 
//  which we can find in the LOOKUP_KEY column of the CONTENT_URI table 
     Cursor cur = getContentResolver().query 
       (ContactsContract.Contacts.CONTENT_URI, PROJECTION, null, null, null); 

     try { 
      if (cur.moveToFirst()) { 
       do { 
        if 
//    If a LOOKUP_KEY value is equal to our look up key string.. 
       (cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)).equalsIgnoreCase(thelookupkey)) { 
//    then delete that LOOKUP_KEY value, including all associated details, like number, name etc... 
         Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, thelookupkey); 
         getContentResolver().delete(uri, null, null); 
        } 

       } while (cur.moveToNext()); 
      } 
//  deal with any errors, should they arise 
     } catch (Exception e) { 
      System.out.println(e.getStackTrace()); 
     } finally { 
//   finally, close the cursor 
      cur.close(); 
     } 

    } 
関連する問題