/*------四大组件--ContentProvider----用来查询手机联系人 * 1,创建MyContentProvider类来继承ContentProvider实现 * 2,利用ContentResolver类接受getContentResolver()返回的对象来实现 * 3,1和2的实现是一样的,他们都有相同的方法 * --------------注意要在AndroidManiFest中加入联系人读取权限---------------------- * */package com.example.s3_contentprovider_demo1;import android.app.Activity;import android.content.ContentResolver;import android.database.Cursor;import android.os.Bundle;import android.provider.ContactsContract;import android.provider.ContactsContract.CommonDataKinds.Email;import android.provider.ContactsContract.CommonDataKinds.Phone;import android.provider.ContactsContract.Contacts;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(); //游标接受查询结果 Cursor c1 = cr.query(Contacts.CONTENT_URI, new String[]{Contacts._ID,Contacts.DISPLAY_NAME}, null, null, null); if (c1!=null) { //c1.moveToFirst(); while(c1.moveToNext()){ int id = c1.getInt(c1.getColumnIndex("_ID")); String name = c1.getString(c1.getColumnIndex("DISPLAY_NAME")); Log.i("xyz", "id: "+id); Log.i("xyz", "name: "+name); Log.i("xyz", "test...1.."); /*--------- 根据联系人的 ID 查询当前联系人的电话号码和类型 -------- */ Cursor c2 = cr.query(Phone.CONTENT_URI, new String[]{Phone.NUMBER,Phone.TYPE}, Phone.CONTACT_ID + "=" + id, null, null); Log.i("xyz", "test...22222.."); if(c2!=null){ //c2.moveToFirst(); while(c2.moveToNext()){ //获得电话类型 用整型 来接收 int type = c2.getInt(c2.getColumnIndex("Phone.TYPE")); if (type==Phone.TYPE_HOME) {//如果是家庭电话 Log.i("xyz", "家庭电话 :"+c2.getString(c2.getColumnIndex("Phone.NUMBER"))); }else if (type==Phone.TYPE_MOBILE) {//如果是移动电话 Log.i("xyz", "手机号码 :"+c2.getString(c2.getColumnIndex("Phone.NUMBER"))); } } c2.close(); } /*--------- 通过联系人的 ID 来查询联系人的email邮箱 ----------*/ Cursor c3 = cr.query(Email.CONTENT_URI,new String[]{Email.DATA,Email.TYPE} , Email.CONTACT_ID+"="+id, null, null); if (c3!=null) { //c3.moveToFirst(); while(c3.moveToNext()){ int EmailType = c3.getInt(c3.getColumnIndex("Email.TYPE")); if (EmailType==Email.TYPE_WORK) {//如果是工作邮箱 Log.i("xyz", "工作邮箱: "+c3.getString(c3.getColumnIndex("Email.DATA"))); } } c3.close(); } } c1.close(); } } }
- 1 回答
- 0 关注
- 1490 浏览
添加回答
举报
0/150
提交
取消