实例,获取联系人信息
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>
本文介绍了如何在Android应用中获取并展示联系人信息,包括电话号码和电子邮件,通过使用ContentResolver查询联系人数据,并利用ExpandableListView进行展示。
514

被折叠的 条评论
为什么被折叠?



