帮我看看,快崩溃了.................
package com.lyxz.contentprovider_demo;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.util.Log;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
* 电话通讯录的查询
*/
// 使用ContentResolver可以操作ContentProvider<增删改查>
ContentResolver cr = getContentResolver();
/*
* 查询方法;第一个属性uri<导Contacts.的新包> 何为Uri? -Uri是指通用资源标志符
* content://com.imooc.provider/music/#
* content://---前缀表明数据受控于一个内容提供者.它从不修改,也就是schema
* com.imooc.provider---是指在AndroidMainfest.
* xml中我们注册的provider中的authorities属性所对应的唯一的 /music---具体操作于哪个条目
* /#---具体指定到哪个条目下的那条记录(#标识通配符)
*
* 第二个属性projection,为需要查询的字段
*
* 后面属性依次为,查询条件,排列顺序
*
* 返回类型为,游标
*/
Cursor c = cr.query(Contacts.CONTENT_URI, new String[] { Contacts._ID, Contacts.DISPLAY_NAME }, null, null, null);
//查询:ID,联系人姓名
if(c != null){
while(c.moveToNext()){
int id = c.getInt(c.getColumnIndex( Contacts._ID));
Log.i("info", "_id"+id);
Log.i("info", c.getString(c.getColumnIndex(Contacts.DISPLAY_NAME)));
//根据ID查询电话号码
Cursor c1 = cr.query(Phone.CONTENT_URI, new String[]{Phone.NUMBER,Phone.CONTENT_TYPE}, Phone.CONTACT_ID+"="+id, null, null);
if(c1 != null){
while(c1.moveToNext()){
int type = c.getInt(c1.getColumnIndex(Phone.CONTENT_TYPE));
if(type == Phone.TYPE_HOME){
Log.i("info", "家庭电话:"+c1.getString(c1.getColumnIndex(Phone.NUMBER)));
}else if(type == Phone.TYPE_MOBILE){
Log.i("info", "手机电话:"+c1.getString(c1.getColumnIndex(Phone.NUMBER)));
}
}
c1.close();
}
//根据ID查询邮箱地址
Cursor c2 = cr.query(Email.CONTENT_URI, new String[]{Email.DATA,Email.TYPE},Email.CONTACT_ID+"="+id, null, null);
if(c2 != null){
while(c2.moveToNext()){
int type = c2.getInt(c2.getColumnIndex(Data.CONTENT_TYPE));
if(type == Email.TYPE_HOME){
Log.i("info","家庭邮编:"+c2.getColumnName(c2.getColumnIndex(Email.DATA)));
}else if(type == Email.TYPE_WORK){
Log.i("info", "工作邮编:"+c2.getColumnName(c2.getColumnIndex(Email.DATA)));
}
}
c2.close();
}
}
c.close();
}
}
}