通过系统自带的内容提供器(ContentResolver)读取系统的通讯录,并设置点击事件
1、布局
主布局只有一个listview,用来显示电话簿的名字和手机号码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="lpc.com.project1722.MainActivity">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</RelativeLayout>
布局2
listview的内容布局,只有两个TextView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:background="#EE00"
android:layout_width="match_parent"
android:layout_height="1dp" />
<TextView
android:id="@+id/name"
android:textSize="25sp"
android:text="我是名字"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/number"
android:textSize="20sp"
android:text="我是号码"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:background="#EE00"
android:layout_width="match_parent"
android:layout_height="1dp" />
</LinearLayout>
2、java文件
package lpc.com.project1722;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ListView list;
private ArrayList<String> nameList = new ArrayList<String>();
private ArrayList<String> numberList = new ArrayList<String>();
myAdapter adapter = new myAdapter();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.list);
readContacts();
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+ numberList.get(position)));
startActivity(intent);
}
});
}
private void readContacts() {
Cursor cursor = null;
try{
cursor = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,null,null,"sort_key asc");
//sort_key asc 这个排序是按照数字,A到Z的顺序排序
//last_time_contacted 最后一次的通话时间,此时为根据通话时间排序
while (cursor.moveToNext()){
String displayName = cursor.getString(cursor.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
nameList.add(displayName);
String number = cursor.getString(cursor.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
numberList.add(number);
}
}catch (Exception e){
e.printStackTrace();
}finally {
cursor.close();
}
}
class myAdapter extends BaseAdapter{
public myAdapter() {
}
@Override
public int getCount() {
return nameList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
View view;
view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.list_item, null);
viewHolder = new ViewHolder();
viewHolder.name = (TextView) view.findViewById(R.id.name);
viewHolder.number = (TextView) view.findViewById(R.id.number);
viewHolder.name.setText(nameList.get(position));
viewHolder.number.setText(numberList.get(position));
return view;
}
class ViewHolder {
public TextView name;
public TextView number;
}
}
}
3、manifest设置
基本上是默认设置,只添加了一个读取 系统联系人的权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="lpc.com.project1722">
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>