1、布局

主布局只有一个listview,用来显示电话簿的名字和手机号码
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:tools="http://schemas.android.com/tools"
    4. android:layout_width="match_parent"
    5. android:layout_height="match_parent"
    6. tools:context="lpc.com.project1722.MainActivity">
    7. <ListView
    8. android:id="@+id/list"
    9. android:layout_width="match_parent"
    10. android:layout_height="match_parent">
    11. </ListView>
    12. </RelativeLayout>

布局2

listview的内容布局,只有两个TextView
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical" android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <ImageView
  6. android:background="#EE00"
  7. android:layout_width="match_parent"
  8. android:layout_height="1dp" />
  9. <TextView
  10. android:id="@+id/name"
  11. android:textSize="25sp"
  12. android:text="我是名字"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content" />
  15. <TextView
  16. android:id="@+id/number"
  17. android:textSize="20sp"
  18. android:text="我是号码"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content" />
  21. <ImageView
  22. android:background="#EE00"
  23. android:layout_width="match_parent"
  24. android:layout_height="1dp" />
  25. </LinearLayout>


2、java文件

  1. package lpc.com.project1722;
  2. import android.content.Intent;
  3. import android.database.Cursor;
  4. import android.net.Uri;
  5. import android.os.Bundle;
  6. import android.provider.ContactsContract;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import android.widget.AdapterView;
  12. import android.widget.BaseAdapter;
  13. import android.widget.ListView;
  14. import android.widget.TextView;
  15. import java.util.ArrayList;
  16. public class MainActivity extends AppCompatActivity {
  17. private ListView list;
  18. private ArrayList<String> nameList = new ArrayList<String>();
  19. private ArrayList<String> numberList = new ArrayList<String>();
  20. myAdapter adapter = new myAdapter();
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.activity_main);
  25. list = (ListView) findViewById(R.id.list);
  26. readContacts();
  27. list.setAdapter(adapter);
  28. list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  29. @Override
  30. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  31. Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+ numberList.get(position)));
  32. startActivity(intent);
  33. }
  34. });
  35. }
  36. private void readContacts() {
  37. Cursor cursor = null;
  38. try{
  39. cursor = getContentResolver().query(
  40. ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
  41. null,null,null,"sort_key asc");
  42. //sort_key asc 这个排序是按照数字,A到Z的顺序排序
  43. //last_time_contacted 最后一次的通话时间,此时为根据通话时间排序
  44. while (cursor.moveToNext()){
  45. String displayName = cursor.getString(cursor.getColumnIndex(
  46. ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
  47. nameList.add(displayName);
  48. String number = cursor.getString(cursor.getColumnIndex(
  49. ContactsContract.CommonDataKinds.Phone.NUMBER));
  50. numberList.add(number);
  51. }
  52. }catch (Exception e){
  53. e.printStackTrace();
  54. }finally {
  55. cursor.close();
  56. }
  57. }
  58. class myAdapter extends BaseAdapter{
  59. public myAdapter() {
  60. }
  61. @Override
  62. public int getCount() {
  63. return nameList.size();
  64. }
  65. @Override
  66. public Object getItem(int position) {
  67. return position;
  68. }
  69. @Override
  70. public long getItemId(int position) {
  71. return position;
  72. }
  73. @Override
  74. public View getView(int position, View convertView, ViewGroup parent) {
  75. ViewHolder viewHolder;
  76. View view;
  77. view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.list_item, null);
  78. viewHolder = new ViewHolder();
  79. viewHolder.name = (TextView) view.findViewById(R.id.name);
  80. viewHolder.number = (TextView) view.findViewById(R.id.number);
  81. viewHolder.name.setText(nameList.get(position));
  82. viewHolder.number.setText(numberList.get(position));
  83. return view;
  84. }
  85. class ViewHolder {
  86. public TextView name;
  87. public TextView number;
  88. }
  89. }
  90. }

3、manifest设置

基本上是默认设置,只添加了一个读取 系统联系人的权限

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="lpc.com.project1722">
  4. <uses-permission android:name="android.permission.READ_CONTACTS"/>
  5. <uses-permission android:name="android.permission.CALL_PHONE"/>
  6. <application
  7. android:allowBackup="true"
  8. android:icon="@mipmap/ic_launcher"
  9. android:label="@string/app_name"
  10. android:supportsRtl="true"
  11. android:theme="@style/AppTheme">
  12. <activity android:name=".MainActivity">
  13. <intent-filter>
  14. <action android:name="android.intent.action.MAIN" />
  15. <category android:name="android.intent.category.LAUNCHER" />
  16. </intent-filter>
  17. </activity>
  18. </application>
  19. </manifest>


通过系统自带的内容提供器(ContentResolver)读取系统的通讯录,并设置点击事件的更多相关文章

  1. android: 内容提供器简介

    我们学了 Android 数据持久化的技术,包括文件存储.SharedPreferences 存 储.以及数据库存储.不知道你有没有发现,使用这些持久化技术所保存的数据都只能在当 前应用程序中访问.虽 ...

  2. Android基础总结(6)——内容提供器

    前面学习的数据持久化技术包括文件存储.SharedPreferences存储以及数据库存储技术保存的数据都只能被当前应用程序所访问.虽然文件存储和SharedPreferences存储中提供了MODE ...

  3. android学习十二(android的Content Provider(内容提供器)的使用)

    文件存储和SharePreference存储以及数据存储一般为了安全,最好用于当前应用程序中訪问和存储数据.内容提供器(Content Provider)主要用于在不同的应用程序之间实现数据共享的功能 ...

  4. Android学习之基础知识十—内容提供器(Content Provider)

    一.跨程序共享数据——内容提供器简介 内容提供器(Content Provider)主要用于在不同的应用程序之间实现数据共享的功能,它提供了一套完整的机制,允许一个程序访问另一个程序中的数据,同时还能 ...

  5. 入职小白随笔之Android四大组件——内容提供器详解(Content Provider)

    Content Provider 内容提供器简介 内容提供器(Content Provider)主要用于在不同的应用程序之间 实现数据共享的功能,它提供了一套完整的机制,允许一个程序访问另一个程序中的 ...

  6. Android入门(十三)内容提供器

    原文链接:http://www.orlion.ga/612/ 内容提供器(Content Provider)主要用于在不同的应用程序之间实现数据共享的功能,它提供了一套完整的机制,允许一个程序访问另一 ...

  7. <Android基础> (七)内容提供器

    第七章 内容提供器 7.1 内容提供器(Content Provider) 主要应用于在不同的应用程序之间实现数据共享功能.允许一个程序访问另一个程序中的数据,同时还能保证被访数据的安全性. 7.2 ...

  8. 5、Android-跨程序共享数据--内容提供器

    Android数据持久化技术:文件存储.SharedPreferences存储.数据库存储 使用这些持久化技术保存的数据只能再当前的应用程序中访问 但是对于不同应用之间的可以实现跨程序数据共享的功能 ...

  9. 内容提供器(ContentProvider)

    一.简介内容提供器(Content Provider)主要用于在不同的应用程序之间实现数据共享的功能,它提供了一套完整的机制,允许一个程序访问另一个程序中的数据,同时还能保证被访数据的安全性.目前,使 ...

随机推荐

  1. oracle大表添加字段default经验分享

    当oracle单表数据量上亿时,对表进行alter table aa add column_1 varchar2(2) defalut 'Y';时,效率及安全性是必须考虑的因素. 本帖以2亿的数据表a ...

  2. AUTOIT解决域控普通用户以管理员身份安装软件方法

    windows域管理,本是很好的管理方式,方便的软件分发,权限控制等功能.不过由于我处软件分发总有那么一些电脑没有成功安装,或是新装的电脑安装软件时漏了安装一些软件,而这些软件需要管理员权限安装的,用 ...

  3. 推荐几个最好用的CRM软件,本人亲测

    CRM是英文Customer Relationship Management 的简写,一般译作“客户关系管理”.CRM最早产生于美国,由Gartner Group 首先提出的CRM这个概念的.20世纪 ...

  4. python3+ 模块学习 之 subprocess

    subprocess 模块方法: call() check_call() check_output() 以上三个方法用于创建一个子进程,父进程等待子进程结束.若shell = true, 则shell ...

  5. 【WCF全析(一)】--服务协定及消息模式

    上周微软开发布会说.NET支持完全跨平台和并开放Core源码的新闻,让我们顿时感到.NET要迎来它的春天.虽然早在几年前.NET就能开发Android和IOS,但是这次的跨平台把Linux都放到了微软 ...

  6. String类常用方法

    1.String类的特点,字符串一旦被初始化就不会被改变. 2.String对象定义的两种方式 ①String s = "affdf";这种定义方式是在字符串常量池中创建一个Str ...

  7. 入门React和Webpack

    最近在学习React.js,之前都是直接用最原生的方式去写React代码,发现组织起来特别麻烦,之前听人说用Webpack组织React组件得心应手,就花了点时间学习了一下,收获颇丰 说说React ...

  8. 重置zend studio 默认设置的方法

    转载自:http://www.zendstudio.net/archives/reset-the-zend-studio-settings/ 这个方法类似于手机的"恢复出厂设置"的 ...

  9. python中x,y交换值的问题

    今天碰到了python和其他语言不同的问题:赋值语句 x,y,z=1,2,3,执行 z,x,y=y,z,x 后,x.y.z 中分别含有什么值? 我想的是 x=2  y=2  z=2 可调试后应该是:x ...

  10. 【转】Java集合类

    http://blog.csdn.net/zhangerqing/article/details/8122075 http://android.blog.51cto.com/268543/400557 ...