Android:使用ContentProvider读取、添加联系人

 1 package com.example.contentproviderdome1;

 2 

 3 import android.os.Bundle;

 4 import android.provider.ContactsContract;

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

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

 7 import android.provider.ContactsContract.Contacts;

 8 import android.app.Activity;

 9 import android.content.ContentResolver;

10 import android.database.Cursor;

11 import android.util.Log;

12 import android.view.Menu;

13 

14 public class MainActivity extends Activity {

15 

16     @Override

17     protected void onCreate(Bundle savedInstanceState) {

18         super.onCreate(savedInstanceState);

19         setContentView(R.layout.activity_main);

20         ContentResolver cr = this.getContentResolver();

21         Cursor c = cr.query(Contacts.CONTENT_URI, new String[]{Contacts._ID,Contacts.DISPLAY_NAME}, null, null, null);

22         if(c!=null){

23             while(c.moveToNext()) {

24                 int id = c.getInt(c.getColumnIndex("_id"));

25                 Log.i("info","id:"+id);

26                 Log.i("info", "name:"+c.getString(c.getColumnIndex(Contacts.DISPLAY_NAME)));

27                 Cursor c1 = cr.query(Phone.CONTENT_URI, new String[]{Phone.NUMBER,Phone.TYPE}, Phone.CONTACT_ID+"="+id, null, null);

28                 if(c1!=null) {

29                     while(c1.moveToNext()){

30                         int type = c1.getInt(c1.getColumnIndex(Phone.TYPE));

31                         if(type == Phone.TYPE_HOME){

32                             Log.i("info","家庭电话:"+c1.getString(c1.getColumnIndex(Phone.NUMBER)));

33                         }else if(type == Phone.TYPE_MOBILE){

34                             Log.i("info","移动电话:"+c1.getString(c1.getColumnIndex(Phone.NUMBER)));

35                         }

36                     }

37                     c1.close();

38                 }

39                 Cursor c2 = cr.query(Email.CONTENT_URI, new String[]{Email.ADDRESS}, Email.CONTACT_ID+"="+id, null, null);

40                 if(c2!=null){

41                     while(c2.moveToNext()){        

42                             Log.i("info", "个人邮箱:"+c2.getString(c2.getColumnIndex(Email.ADDRESS)));

43                         }        

44                     c2.close();

45                 }

46             }

47             c.close();

48         }

49     }

50 }

上面的是读取。读取电子邮件也可用Email.DATA代替Email.ADDRESS,添加TPYE实现分类。

最后不要忘了在Manifest文件中添加权限!

下面的是插入。

 1 package com.example.contentproviderdome2;

 2 

 3 import android.app.Activity;

 4 import android.content.ContentResolver;

 5 import android.content.ContentUris;

 6 import android.content.ContentValues;

 7 import android.net.Uri;

 8 import android.os.Bundle;

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

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

11 import android.provider.ContactsContract.RawContacts;

12 import android.provider.ContactsContract.Data;

13 import android.util.Log;

14 

15 public class MainActivity extends Activity {

16 

17     @Override

18     protected void onCreate(Bundle savedInstanceState) {

19         super.onCreate(savedInstanceState);

20         setContentView(R.layout.activity_main);

21         ContentResolver cr = getContentResolver();

22         ContentValues values = new ContentValues();

23         //插入一条数据

24         Uri uri = cr.insert(RawContacts.CONTENT_URI, values);

25         Long raw_contact_id = ContentUris.parseId(uri);

26         values.clear();

27         //插入人名

28         values.put(StructuredName.CONTACT_ID, raw_contact_id);    

29         values.put(StructuredName.DISPLAY_NAME, "abc");    

30         Log.i("info",""+raw_contact_id);

31         values.put(StructuredName.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);

32         Log.i("info",""+raw_contact_id);

33         uri = cr.insert(Data.CONTENT_URI, values);

34         Log.i("info",""+raw_contact_id);

35         values.clear();

36         //插入电话

37         values.put(Phone.CONTACT_ID, raw_contact_id);

38         values.put(Phone.NUMBER, "1388888888");

39         values.put(Phone.MIMETYPE, Phone.CONTENT_ITEM_TYPE);

40         uri = cr.insert(Data.CONTENT_URI, values);

41         values.clear();

42     }

43 }

程序崩溃。前两个Log可以打印,但是第三个不行。错误日志显示空指针。一共有5个Data类,只有1个有CONTENT_URI,跟教程中的源码对照没发现区别,权限也加了,不知道为什么。

 

你可能感兴趣的:(ContentProvider)