使用系统ContentProvider获取联系人信息
2014-03-30 17:57
381 查看
实例,获取联系人信息
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<Button android:id="@+id/searchBut"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="查看"/>
</LinearLayout>
result.xml
(弹出联系人界面)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ExpandableListView android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ExpandableListView>
</LinearLayout>
MainActivity.java
package com.example.contactprovidertest;
import java.util.ArrayList;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.app.Activity;
import android.app.AlertDialog;
import android.database.Cursor;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
Button searchBut=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
searchBut=(Button) super.findViewById(R.id.searchBut);
searchBut.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//定义两个List来凤爪联系人的信息, 一个是获取所有联系人,一个是联系人的详细信息
final ArrayList<String> names=new ArrayList<String>();
final ArrayList<ArrayList<String>> details=new ArrayList<ArrayList<String>>();
//使用ContentResolver查找联系人的数据
Cursor cursor=getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
//遍历查询结果,获取系统中所有的联系人
while(cursor.moveToNext()){
//获取联系人的Id
String contactId=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
//获取联系人的名字
String contactName=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
names.add(contactName);
//使用ContentResolver查找联系人的电话号码
Cursor phones=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+contactId, null, null);
ArrayList<String> detail=new ArrayList<String>();
while(phones.moveToNext()){
//获取电话号码
String phoneNum=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
detail.add("电话号码:"+phoneNum);
}
phones.close();
//使用ContentResolver获得联系人的邮件
Cursor emails=getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID+"="+contactId, null, null);
while(emails.moveToNext()){
//获取Email
String email=emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
detail.add("邮件:"+email);
}
emails.close();
details.add(detail);
}
cursor.close();
//加载result.xml界面布局代表的视图
View resultDialog=getLayoutInflater().inflate(R.layout.result, null);
//获取我们resultDialog里边的ExpandableListAdapter
ExpandableListView list=(ExpandableListView) resultDialog.findViewById(R.id.list);
//创建一个ExpandableListAdapter对象
ExpandableListAdapter adapter=new BaseExpandableListAdapter() {
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
LinearLayout ll=new LinearLayout(MainActivity.this);
ll.setOrientation(LinearLayout.HORIZONTAL);
TextView textView=getTextView();
textView.setText(names.get(groupPosition));
ll.addView(textView);
return ll;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public int getGroupCount() {
return names.size();
}
@Override
public Object getGroup(int groupPosition) {
return names.get(groupPosition);
}
@Override
public int getChildrenCount(int groupPosition) {
return details.get(groupPosition).size();
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
LinearLayout ll=new LinearLayout(MainActivity.this);
ll.setOrientation(LinearLayout.HORIZONTAL);
TextView textView=getTextView();
textView.setText(details.get(groupPosition).get(childPosition));
ll.addView(textView);
return ll;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return details.get(groupPosition).get(childPosition);
}
};
list.setAdapter(adapter);
//使用对话框显示结果
new AlertDialog.Builder(MainActivity.this).setView(resultDialog).setPositiveButton("确定", null).show();
}
});
}
//创建TextView的方法
public TextView getTextView(){
LinearLayout.LayoutParams ll=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 86);
TextView view=new TextView(MainActivity.this);
view.setGravity(Gravity.CENTER_VERTICAL);
view.setPadding(50, 0, 0, 0);
view.setLayoutParams(ll);
view.setTextSize(18);
return view;
}
}
AndroidManifest.xml
加上对联系人处理的权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.contactprovidertest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.contactprovidertest.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
</manifest>
相关文章推荐
- 使用ContentProvider获取联系人的所有信息
- Android(java)学习笔记249:ContentProvider使用之获得系统联系人信息01
- 使用系统内置的ContentProvider获取非SIM卡的手机联系人
- 无废话Android之内容观察者ContentObserver、获取和保存系统的联系人信息、网络图片查看器、网络html查看器、使用异步框架Android-Async-Http(4)
- 使用ContentProvider读取系统联系人信息
- Android(java)学习笔记250:ContentProvider使用之获得系统联系人信息02(掌握)
- ContentProvider获取系统的电话号码绑定到ListView以及跳转通讯录获区联系人信息绑定到前台。
- 使用python获取系统网络信息
- 获取系统联系人信息
- 使用Environment 来获取系统的信息
- 使用Java实现B/S系统常见的获取客户端信息功能!
- android-ContentProvider获取联系人信息
- 使用ContentProvider添加获取通讯录信息
- 如何使用sigar包获取系统信息
- 使用Java获取系统信息的常用代码整理总结
- Android四大组件之ContentProvider获取系统的联系人及联系人电话
- 使用getrusage获取进程使用系统资源信息
- [转]排查线程池使用情况,可定时任务统计.打印到指定日志. 获取系统扩容,线程池改造信息.
- 获取系统联系人信息
- Android中获取系统内存信息以及进程信息-----ActivityManager的使用(一)