用ContentProvider向系统增加联系人

发现对系统的联系人进行操作的api很乱,感觉逻辑有点不清楚......

主要用到这4个类:

android.provider.ContactsContract.CommonDataKinds.Email;
android.provider.ContactsContract.CommonDataKinds.Phone;
android.provider.ContactsContract.CommonDataKinds.StructuredName;
android.provider.ContactsContract.Data;

 1 import android.content.ContentResolver;

 2 import android.content.ContentUris;

 3 import android.content.ContentValues;

 4 import android.net.Uri;

 5 import android.os.Bundle;

 6 import android.provider.ContactsContract.CommonDataKinds.Email;

 7 import android.provider.ContactsContract.CommonDataKinds.Phone;

 8 import android.provider.ContactsContract.CommonDataKinds.StructuredName;

 9 import android.provider.ContactsContract.Data;

10 import android.provider.ContactsContract.RawContacts;

11 import android.support.v7.app.ActionBarActivity;

12 import android.util.Log;

13 import android.view.Menu;

14 import android.view.MenuItem;

15 import android.view.View;

16 import android.widget.Button;

17 import android.widget.EditText;

18 

19 public class MainActivity extends ActionBarActivity {

20     ContentResolver contentResolver;

21     EditText name;

22     EditText phone;

23     EditText emil;

24 

25     @Override

26     protected void onCreate(Bundle savedInstanceState) {

27         super.onCreate(savedInstanceState);

28         setContentView(R.layout.activity_main);

29         name = (EditText) findViewById(R.id.editText_name);

30         phone = (EditText) findViewById(R.id.EditText_phone);

31         emil = (EditText) findViewById(R.id.editText_emil);

32         contentResolver = getContentResolver();

33         Button saveButton = (Button) findViewById(R.id.button_insert);

34         saveButton.setOnClickListener(new MyOnclickListener());

35 

36     }

37 

38     class MyOnclickListener implements View.OnClickListener {

39 

40         @Override

41         public void onClick(View v) {

42             ContentValues values = new ContentValues();

43             Uri rawContactUri = contentResolver.insert(RawContacts.CONTENT_URI, values);//插入一个空值,目的是增加一条记录,返回该记录的id

44             long rawContactId = ContentUris.parseId(rawContactUri);

45             Log.i("csx", "rawContactId:" + rawContactId);

46             // 插入姓名

47             values.clear();

48             values.put(Data.RAW_CONTACT_ID, rawContactId);

49             values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);

50             values.put(StructuredName.GIVEN_NAME, name.getText().toString());

51             contentResolver.insert(Data.CONTENT_URI, values);

52             // 插入电话

53             values.clear();

54             values.put(Data.RAW_CONTACT_ID, rawContactId);

55             values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);

56             values.put(Phone.NUMBER, phone.getText().toString());

57             values.put(Phone.TYPE, Phone.TYPE_MOBILE);

58             contentResolver.insert(Data.CONTENT_URI, values);

59             // 插入邮箱

60             values.clear();

61             values.put(Data.RAW_CONTACT_ID, rawContactId);

62             values.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);

63             values.put(Email.DATA, emil.getText().toString());

64             values.put(Email.TYPE, Email.TYPE_WORK);

65             contentResolver.insert(Data.CONTENT_URI, values);

66 

67         }

68     }

 

你可能感兴趣的:(ContentProvider)