Insert contact in Android with ContactsContract

后端 未结 4 836
太阳男子
太阳男子 2020-12-08 11:33

I am trying to add a new contact to the Android 2.2 contacts directly.

//this code doesn\'t work    
ContentValues cv=new ContentValues();
c         


        
4条回答
  •  执笔经年
    2020-12-08 12:26

    ContentValues cv = new ContentValues();
    cv.put(People.NAME, e1.getText().toString());
    
    // e1.getText().tostring() is Contact name 
    
    Uri u =    getContentResolver().insert(People.CONTENT_URI, cv);
    
    Uri pathu = Uri.withAppendedPath(u, People.Phones.CONTENT_DIRECTORY);    
    
    cv.clear();
    
    cv.put(People.NUMBER, e2.getText().toString());
    
    // e2.getText().tostring() is Contact number
    
    getContentResolver().insert(pathu, cv);
    
    Toast.makeText(getApplicationContext(), "Contact Added",Toast.LENGTH_LONG).show();
    

提交回复
热议问题