通过系统自带的内容提供器(ContentResolver)读取系统的通讯录,并设置点击事件
1、布局
<?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>
<?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>
通过系统自带的内容提供器(ContentResolver)读取系统的通讯录,并设置点击事件的更多相关文章
- android: 内容提供器简介
我们学了 Android 数据持久化的技术,包括文件存储.SharedPreferences 存 储.以及数据库存储.不知道你有没有发现,使用这些持久化技术所保存的数据都只能在当 前应用程序中访问.虽 ...
- Android基础总结(6)——内容提供器
前面学习的数据持久化技术包括文件存储.SharedPreferences存储以及数据库存储技术保存的数据都只能被当前应用程序所访问.虽然文件存储和SharedPreferences存储中提供了MODE ...
- android学习十二(android的Content Provider(内容提供器)的使用)
文件存储和SharePreference存储以及数据存储一般为了安全,最好用于当前应用程序中訪问和存储数据.内容提供器(Content Provider)主要用于在不同的应用程序之间实现数据共享的功能 ...
- Android学习之基础知识十—内容提供器(Content Provider)
一.跨程序共享数据——内容提供器简介 内容提供器(Content Provider)主要用于在不同的应用程序之间实现数据共享的功能,它提供了一套完整的机制,允许一个程序访问另一个程序中的数据,同时还能 ...
- 入职小白随笔之Android四大组件——内容提供器详解(Content Provider)
Content Provider 内容提供器简介 内容提供器(Content Provider)主要用于在不同的应用程序之间 实现数据共享的功能,它提供了一套完整的机制,允许一个程序访问另一个程序中的 ...
- Android入门(十三)内容提供器
原文链接:http://www.orlion.ga/612/ 内容提供器(Content Provider)主要用于在不同的应用程序之间实现数据共享的功能,它提供了一套完整的机制,允许一个程序访问另一 ...
- <Android基础> (七)内容提供器
第七章 内容提供器 7.1 内容提供器(Content Provider) 主要应用于在不同的应用程序之间实现数据共享功能.允许一个程序访问另一个程序中的数据,同时还能保证被访数据的安全性. 7.2 ...
- 5、Android-跨程序共享数据--内容提供器
Android数据持久化技术:文件存储.SharedPreferences存储.数据库存储 使用这些持久化技术保存的数据只能再当前的应用程序中访问 但是对于不同应用之间的可以实现跨程序数据共享的功能 ...
- 内容提供器(ContentProvider)
一.简介内容提供器(Content Provider)主要用于在不同的应用程序之间实现数据共享的功能,它提供了一套完整的机制,允许一个程序访问另一个程序中的数据,同时还能保证被访数据的安全性.目前,使 ...
随机推荐
- WinForm控件TreeView 只部分节点显示 CheckBox
WinForm控件TreeView 只部分节点显示 CheckBox 用过asp.net的应该知道,要在treeview中实现上述功能可以使用ShowCheckBox 属性指定那些节点显示check ...
- FTP地址格式如下:“ftp://用户名:密码@FTP服务器IP”
FTP地址格式如下:“ftp://用户名:密码@FTP服务器IP”
- mysql load data 乱码的问题
新学mysql在用load data导入txt文档时发现导入的内容,select 之后是乱码,先后把表,数据库的字符集类型修改为utf8,但还是一样,最后在 http://bbs.chinaunix. ...
- Android自动化学习笔记:编写MonkeyRunner脚本的几种方式
---------------------------------------------------------------------------------------------------- ...
- Openstack Day1简介及虚拟环境搭建
本文章仅作为作者本人存档记忆!恕不详细展开内容! openstack kilo版本重要组件(module)简介 ======================================= Ke ...
- SQL计算时间差,要精确到小时分钟秒
declare @starttime as datetime declare @endtime as datetime set @starttime = '2009-11-24 15:10:00' s ...
- crontab 提示 command not found 解决方案
今天遇见一个问题,crontab的定时任务会报错:java command not found,但是手动执行脚本一直能成功. 猜想是环境变量的问题. 在crontab里添加个打印环境变量的任务: * ...
- Maximo-获取url
//访问报表public void OPENREPORT() throws RemoteException, MXException{ int polineid=this.getMbo().getIn ...
- modelsim(3) - summary ,issue,tips
1) the OEM of modelsim is 10 times slower than offical questa 2)how to the file full path in the mod ...
- NTFS u盘支持 - centos6
uname -r bash-4.1# uname -r2.6.32-431.el6.x86_64 ---- http://repository.it4i.cz/mirrors/repoforge/re ...