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. 在线编辑器的使用总结(kindeditor , )

    1).kindedtor中让编辑框默认为“HTML代码/源代码”模式 [javascript] view plaincopyprint? <script> // 自定义插件 #1 Kind ...

  2. ASP.NET 服务器控件的生命周期

    服务器控件生命周期简介 服务器控件的生命周期是创建服务器控件最重要的概念.作为开发人员,必须对服务器控件生命周期深刻理解.当然,这不是一朝一夕就可以做到的.对于学习控件开发技术的初学者,可以不必掌握得 ...

  3. python 核心编程课后练习(chapter 5)

    5-2 #5-2 def mul(x, y): return x * y print mul(4,5) 5-3 #5-3 def value_score(num): if 90<=num< ...

  4. IOS 中openGL使用教程1(openGL ES 入门篇 | 搭建openGL环境)

    OpenGL版本 iOS系统默认支持OpenGl ES1.0.ES2.0以及ES3.0 3个版本,三者之间并不是简单的版本升级,设计理念甚至完全不同,在开发OpenGL项目前,需要根据业务需求选择合适 ...

  5. .NET跨AppDomain访问对象

    什么是AppDomain? 我们都知道windows进程,它起到应用程序隔离的作用,带来的好处是,当某个进程发生错误的时候,不会影响其他的进程,系统也不会受到影响.但是,创建windows进程的代价是 ...

  6. 二模15day1解题报告

    T1.合并序列(minval) 给出长为 n的AB两个序列求两两相加中最小的n个. 据说有证明(在蓝书上,优先队列部分)先把A[1~n]+b[1]入队,然后每取一个a[x]+b[y]就把a[x]+b[ ...

  7. ContentProvider小结

    1.什么情况下需要使用ContentProvider 跨进程提供数据访问的接口,如果在同一个App下,没有必要使用此种方式 2.自定义ContentProvider public class MyCo ...

  8. Objective C ARC 使用及原理

    手把手教你ARC ,里面介绍了ARC的一些特性, 还有将非ARC工程转换成ARC工程的方法 ARC 苹果官方文档 下面用我自己的话介绍一下ARC,并将看文档过程中的疑问和答案写下来.下面有些是翻译,但 ...

  9. Axure 7.0 正式版 + 汉化包 安装

    详情如下: Axure 7.0 正式版终于发布了,现在提供简体中文版给大家使用. Axure 7.0 正式版: 链接: http://pan.baidu.com/s/1kV4OJ47 提取密码: be ...

  10. C语言PIC18 serial bootloader和C#语言bootloader PC端串口通信程序

    了解更多关于bootloader 的C语言实现,请加我QQ: 1273623966 (验证信息请填 bootloader),欢迎咨询或定制bootloader(在线升级程序). 新PIC18 Boot ...