package com.example.contacts_demo2;
import com.example.contacts_demo.AddressActivity;
import com.example.contacts_demo.R;
import com.example.contacts_demo.R.anim;
import com.example.contacts_demo.R.id;
import com.example.contacts_demo.R.layout;
import com.example.contacts_demo.R.menu;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.RawContacts;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class AddActivity extends Activity {
// 文本域定义
private EditText editname, editphone, edtemali;
// 按钮定义
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_add);
// 获取文本域的id
editname = (EditText) findViewById(R.id.editText1);
editphone = (EditText) findViewById(R.id.editText2);
edtemali = (EditText) findViewById(R.id.editText3);
button = (Button) findViewById(R.id.but);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// value方法
ContentValues values = new ContentValues();
// 将文本域内容转换成String 类型
String name = editname.getText().toString();
String number = editphone.getText().toString();
String email = edtemali.getText().toString();
ContentResolver resolver = getContentResolver();
// 首先向RawContacts.CONTENT_URI执行一个空值插入,目的是获取系统返回的rawContactId
Uri rawContactUri = resolver.insert(RawContacts.CONTENT_URI,
values);
long rawContactId = ContentUris.parseId(rawContactUri);
// 往data表入姓名数据
if (!name.equals("")) {
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);// 内容类型
values.put(StructuredName.GIVEN_NAME, name);
resolver.insert(
android.provider.ContactsContract.Data.CONTENT_URI,
values);
} else {
Toast.makeText(AddActivity.this, "姓名不能为空",
Toast.LENGTH_LONG).show();
}
// 往data表入电话数据
if (!number.equals("")) {
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Phone.NUMBER, number);
values.put(Phone.TYPE, Phone.TYPE_MOBILE);
resolver.insert(ContactsContract.Data.CONTENT_URI, values);
} else {
Toast.makeText(AddActivity.this, "电话号码不能为空",
Toast.LENGTH_LONG).show();
}
// 往date表内插入电子邮箱
if (!email.equals("")) {
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);// 内容类型
values.put(Email.DATA, email);
values.put(Email.TYPE, Email.TYPE_WORK);
resolver.insert(
android.provider.ContactsContract.Data.CONTENT_URI,
values);
} else {
Toast.makeText(AddActivity.this, "電子郵箱不能为空",
Toast.LENGTH_LONG).show();
}
}
});
}
import com.example.contacts_demo.AddressActivity;
import com.example.contacts_demo.R;
import com.example.contacts_demo.R.anim;
import com.example.contacts_demo.R.id;
import com.example.contacts_demo.R.layout;
import com.example.contacts_demo.R.menu;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.RawContacts;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class AddActivity extends Activity {
// 文本域定义
private EditText editname, editphone, edtemali;
// 按钮定义
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_add);
// 获取文本域的id
editname = (EditText) findViewById(R.id.editText1);
editphone = (EditText) findViewById(R.id.editText2);
edtemali = (EditText) findViewById(R.id.editText3);
button = (Button) findViewById(R.id.but);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// value方法
ContentValues values = new ContentValues();
// 将文本域内容转换成String 类型
String name = editname.getText().toString();
String number = editphone.getText().toString();
String email = edtemali.getText().toString();
ContentResolver resolver = getContentResolver();
// 首先向RawContacts.CONTENT_URI执行一个空值插入,目的是获取系统返回的rawContactId
Uri rawContactUri = resolver.insert(RawContacts.CONTENT_URI,
values);
long rawContactId = ContentUris.parseId(rawContactUri);
// 往data表入姓名数据
if (!name.equals("")) {
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);// 内容类型
values.put(StructuredName.GIVEN_NAME, name);
resolver.insert(
android.provider.ContactsContract.Data.CONTENT_URI,
values);
} else {
Toast.makeText(AddActivity.this, "姓名不能为空",
Toast.LENGTH_LONG).show();
}
// 往data表入电话数据
if (!number.equals("")) {
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Phone.NUMBER, number);
values.put(Phone.TYPE, Phone.TYPE_MOBILE);
resolver.insert(ContactsContract.Data.CONTENT_URI, values);
} else {
Toast.makeText(AddActivity.this, "电话号码不能为空",
Toast.LENGTH_LONG).show();
}
// 往date表内插入电子邮箱
if (!email.equals("")) {
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);// 内容类型
values.put(Email.DATA, email);
values.put(Email.TYPE, Email.TYPE_WORK);
resolver.insert(
android.provider.ContactsContract.Data.CONTENT_URI,
values);
} else {
Toast.makeText(AddActivity.this, "電子郵箱不能为空",
Toast.LENGTH_LONG).show();
}
}
});
}