Android uses content providers to add Contacts. android providers
In the previous article, "Android getting contacts from the system" mainly describes how to obtain and practice system contacts. This article will introduce how to add a contact information
Add contact
1. Add the raw_contacts table and add a contact id.
2. Operate the data table and insert the contact data. Each contact data corresponds to a raw_contact_id column.
The layout file code for creating a project is as follows:
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical" tools: context = ". mainActivity "> <EditText android: id =" @ + id/et_name "android: layout_width =" match_parent "android: layout_height =" wrap_content "android: hint = "Enter the contact name"/> <EditText android: id = "@ + id/et_phone" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: hint = "Enter the contact's phone number"/> <EditText android: id = "@ + id/et_email" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: hint = "Enter the contact's email address"/> <Button android: layout_width = "match_parent" android: layout_height = "wrap_content" android: onClick = "save" android: text = "submit data"/> </LinearLayout>
Add related Code
Package com. wuyudong. addcontact; import android.net. uri; import android. OS. bundle; import android. app. activity; import android. content. contentResolver; import android. content. contentValues; import android. database. cursor; import android. text. textUtils; import android. view. view; import android. widget. editText; import android. widget. toast; public class MainActivity extends Activity {private EditText et_name; private EditText et_phone; private EditText et_mail; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); et_name = (EditText) findViewById (R. id. et_name); et_phone = (EditText) findViewById (R. id. et_phone); et_mail = (EditText) findViewById (R. id. et_email);} public void save (View view) {String name = et_name.getText (). toString (). trim (); String phone = et_phone.getText (). toString (). trim (); String email = et_mail.getText (). toString (). trim (); if (TextUtils. isEmpty (name) | TextUtils. isEmpty (phone) | TextUtils. isEmpty (email) {Toast. makeText (this, "content cannot be blank", 0 ). show ();} else {// save data ContentResolver resolver = getContentResolver (); Uri uri Uri = Uri. parse ("content: // com. android. contacts/raw_contacts "); Uri datauri = Uri. parse ("content: // com. android. contacts/data "); Cursor cursor = resolver. query (uri, null, null); int count = cursor. getCount (); ContentValues values = new ContentValues (); int contact_id = count + 1; values. put ("contact_id", contact_id); resolver. insert (uri, values); // 2. insert specific data to the data table, data type, data1 specific data ContentValues namevalue = new ContentValues (); namevalue. put ("mimetype", "vnd. android. cursor. item/name "); // specifies the Data Type namevalue. put ("data1", name); namevalue. put ("raw_contact_id", contact_id); // you must specify the contact resolver that the data belongs. insert (datauri, namevalue); ContentValues emailvalue = new ContentValues (); emailvalue. put ("mimetype", "vnd. android. cursor. item/email_v2 "); emailvalue. put ("data1", email); emailvalue. put ("raw_contact_id", contact_id); resolver. insert (datauri, emailvalue); ContentValues phonevalue = new ContentValues (); phonevalue. put ("mimetype", "vnd. android. cursor. item/phone_v2 "); phonevalue. put ("data1", phone); phonevalue. put ("raw_contact_id", contact_id); resolver. insert (datauri, phonevalue); Toast. makeText (this, "added successfully", 0 ). show ();}}}