Android通讯录管理(获取联系人、通话记录、短信消息)(二)

前言:上一篇博客介绍的是获取联系人的实现,本篇博客将介绍通话记录的实现。

界面布局:

/Contact_Demo/res/layout/contact_record_list_view.xml

android:id="@+id/contact_record_view"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#000000">

android:id="@+id/call_log_list"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_alignParentTop="true"

android:cacheColorHint="#000000"

android:fadingEdge="none"

android:scrollingCache="false"

android:visibility="visible" />

/Contact_Demo/res/layout/contact_record_list_item.xml

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="vertical" >

android:id="@+id/call_type"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerVertical="true"

android:layout_marginLeft="5dip"

android:layout_marginRight="5dip"

android:background="@drawable/ic_calllog_outgoing_nomal" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerVertical="true"

android:layout_toRightOf="@+id/call_type"

android:orientation="vertical" >

android:id="@+id/name"

android:layout_width="wrap_content"

android:layout_height="0dip"

android:layout_weight="1"

android:textAppearance="?android:textAppearanceMedium"

android:textColor="#ffffff" />

android:id="@+id/number"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textAppearance="?android:textAppearanceSmall"

android:textColor="#cccccc" />

android:id="@+id/call_btn"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentRight="true"

android:layout_centerVertical="true"

android:layout_marginLeft="10dip"

android:layout_marginRight="10dip"

android:background="@drawable/ic_calllog_call_btn" />

android:id="@+id/fg"

android:layout_width="wrap_content"

android:layout_height="75dip"

android:layout_toLeftOf="@+id/call_btn"

android:background="@drawable/black_bg" />

android:id="@+id/time"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerVertical="true"

android:layout_toLeftOf="@+id/fg"

android:textColor="#ffffff" />

定义实体类:

/Contact_Demo/src/com/suntek/contact/model/CallLogBean.java

package com.suntek.contact.model;

/**

* 通话记录实体类

*

* @author Administrator

*

*/

public class CallLogBean {

private int id;

private String name; // 名称

private String number; // 号码

private String date; // 日期

private int type; // 来电:1,拨出:2,未接:3

private int count; // 通话次数

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getNumber() {

return number;

}

public void setNumber(String number) {

this.number = number;

}

public String getDate() {

return date;

}

public void setDate(String date) {

this.date = date;

}

public int getType() {

return type;

}

public void setType(int type) {

this.type = type;

}

public int getCount() {

return count;

}

public void setCount(int count) {

this.count = count;

}

}

/Contact_Demo/src/com/suntek/contact/adapter/DialAdapter.java

package com.suntek.contact.adapter;

import java.util.List;

import android.content.Context;

import android.content.Intent;

import android.net.Uri;

import android.view.LayoutInflater;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.ImageView;

import android.widget.TextView;

import com.suntek.contact.R;

import com.suntek.contact.model.CallLogBean;

/**

* 电话记录适配器

*

* @author Administrator

*

*/

public class DialAdapter extends BaseAdapter {

private Context ctx;

private List callLogs;

private LayoutInflater inflater;

public DialAdapter(Context context, List callLogs) {

this.ctx = context;

this.callLogs = callLogs;

this.inflater = LayoutInflater.from(context);

}

@Override

public int getCount() {

return callLogs.size();

}

@Override

public Object getItem(int position) {

return callLogs.get(position);

}

@Override

public long getItemId(int position) {

return position;

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder;

if (convertView == null) {

convertView = inflater.inflate(R.layout.contact_record_list_item,

null);

holder = new ViewHolder();

holder.call_type = (ImageView) convertView

.findViewById(R.id.call_type);

holder.name = (TextView) convertView.findViewById(R.id.name);

holder.number = (TextView) convertView.findViewById(R.id.number);

holder.time = (TextView) convertView.findViewById(R.id.time);

holder.call_btn = (TextView) convertView

.findViewById(R.id.call_btn);

convertView.setTag(holder); // 缓存

} else {

holder = (ViewHolder) convertView.getTag();

}

CallLogBean callLog = callLogs.get(position);

switch (callLog.getType()) {

case 1:

holder.call_type

.setBackgroundResource(R.drawable.ic_calllog_outgoing_nomal);

break;

case 2:

holder.call_type

.setBackgroundResource(R.drawable.ic_calllog_incomming_normal);

break;

case 3:

holder.call_type

.setBackgroundResource(R.drawable.ic_calllog_missed_normal);

break;

}

holder.name.setText(callLog.getName());

holder.number.setText(callLog.getNumber());

holder.time.setText(callLog.getDate());

addViewListener(holder.call_btn, callLog, position);

return convertView;

}

private static class ViewHolder {

ImageView call_type;

TextView name;

TextView number;

TextView time;

TextView call_btn;

}

private void addViewListener(View view, final CallLogBean callLog,

final int position) {

view.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

Uri uri = Uri.parse("tel:" + callLog.getNumber());

Intent intent = new Intent(Intent.ACTION_CALL, uri);

ctx.startActivity(intent);

}

});

}

}

/Contact_Demo/src/com/suntek/contact/ContactRecordListActivity.java

package com.suntek.contact;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Date;

import java.util.List;

import android.app.Activity;

import android.content.AsyncQueryHandler;

import android.content.ContentResolver;

import android.database.Cursor;

import android.net.Uri;

import android.os.Bundle;

import android.provider.CallLog;

import android.widget.ListView;

import com.suntek.contact.adapter.DialAdapter;

import com.suntek.contact.model.CallLogBean;

/**

* 通话记录列表

*

* @author wwj

*

*/

public class ContactRecordListActivity extends Activity {

private ListView callLogListView;

private AsyncQueryHandler asyncQuery;

private DialAdapter adapter;

private List callLogs;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.contact_record_list_view);

callLogListView = (ListView) findViewById(R.id.call_log_list);

asyncQuery = new MyAsyncQueryHandler(getContentResolver());

init();

}

private void init() {

Uri uri = android.provider.CallLog.Calls.CONTENT_URI;

// 查询的列

String[] projection = { CallLog.Calls.DATE, // 日期

CallLog.Calls.NUMBER, // 号码

CallLog.Calls.TYPE, // 类型

CallLog.Calls.CACHED_NAME, // 名字

CallLog.Calls._ID, // id

};

asyncQuery.startQuery(0, null, uri, projection, null, null,

CallLog.Calls.DEFAULT_SORT_ORDER);

}

private class MyAsyncQueryHandler extends AsyncQueryHandler {

public MyAsyncQueryHandler(ContentResolver cr) {

super(cr);

}

@Override

protected void onQueryComplete(int token, Object cookie, Cursor cursor) {

if (cursor != null && cursor.getCount() > 0) {

callLogs = new ArrayList();

SimpleDateFormat sfd = new SimpleDateFormat("MM-dd hh:mm");

Date date;

cursor.moveToFirst(); // 游标移动到第一项

for (int i = 0; i < cursor.getCount(); i++) {

cursor.moveToPosition(i);

date = new Date(cursor.getLong(cursor

.getColumnIndex(CallLog.Calls.DATE)));

String number = cursor.getString(cursor

.getColumnIndex(CallLog.Calls.NUMBER));

int type = cursor.getInt(cursor

.getColumnIndex(CallLog.Calls.TYPE));

String cachedName = cursor.getString(cursor

.getColumnIndex(CallLog.Calls.CACHED_NAME));// 缓存的名称与电话号码,如果它的存在

int id = cursor.getInt(cursor

.getColumnIndex(CallLog.Calls._ID));

CallLogBean callLogBean = new CallLogBean();

callLogBean.setId(id);

callLogBean.setNumber(number);

callLogBean.setName(cachedName);

if (null == cachedName || "".equals(cachedName)) {

callLogBean.setName(number);

}

callLogBean.setType(type);

callLogBean.setDate(sfd.format(date));

callLogs.add(callLogBean);

}

if (callLogs.size() > 0) {

setAdapter(callLogs);

}

}

super.onQueryComplete(token, cookie, cursor);

}

}

private void setAdapter(List callLogs) {

adapter = new DialAdapter(this, callLogs);

callLogListView.setAdapter(adapter);

}

}

代码是最好的解释了,这里使用的几个重要的类,一个是Uri(进行查询的通用资源标志符),一个是AsyncQueryHandler(Android提供的异步操作数据库的类),这里我们调用它的startQuery方法来查询数据库,在它onQueryComplete方法中得到数据库返回的游标cousor,通过curor来取得数据库对应表中的字段值。

android通讯录管理(获取联系人,通话记录,短信消息),Android通讯录管理(获取联系人、通话记录、短信消息)(二)...相关推荐

  1. android 获取通讯录全选反选_Xamarin.Forms读取并展示Android和iOS通讯录 TerminalMACS客户端...

    本文同步更新地址: https://dotnet9.com/11520.html https://terminalmacs.com/861.html 阅读导航: 一.功能说明 二.代码实现 三.源码获 ...

  2. 玩下软工项目,第一轮--全局Context的获取,SQLite的建立与增删改查,读取用户通话记录信息...

    项目的Github地址:https://github.com/ggrcwxh/LastTime 采用基于git的多人协作开发模式 软件采用mvc设计模式,前端这么艺术的事我不太懂,交给斌豪同学去头疼了 ...

  3. android 好友字母分组,Android好友联系人按字母排序分组,自定义通讯录导航栏View...

    Android中仿微信实现联系人列表 按字母排序分组 自定义通讯录导航栏View,下边是效果图: 1. 自定义View public class SideBar extends View { // 触 ...

  4. Android好友联系人按字母排序分组,自定义通讯录导航栏View

    Android中仿微信实现联系人列表 按字母排序分组 自定义通讯录导航栏View,下边是效果图: 1. 自定义View public class SideBar extends View {// 触摸 ...

  5. android 手机短信恢复,Android短信如何恢复

    Android短信如何恢复?虽然当下微信使用非常普及,但不少重要事项还是会使用短信进行沟通的,比如快递密码箱ID提醒.信用卡还款提醒.验证码等.其目的是确保一定收到,不会因断网.未登陆等消息消失.所以 ...

  6. Android应用生死轮回的那些事儿(3) - 武器库(1)-权限管理相关API

    Android应用生死轮回的那些事儿(3) - 武器库(1)-权限管理相关API PackageManager中提供的武器,可以用"既多又杂,版本变化大"来形容. 不过,我们通过分 ...

  7. Android FrameWork(AMS,WMS,PMS等)的概念及解析,获取系统服务

    Framework API: Activity Manager/Window Manager/Content Providers/View System/Notification Manager/Pa ...

  8. android 短信超链接,Android处理网页的短信链接

    最近遇到了很多网页,尤其是通过短信找回密码的网站,其规范格式为106659999,但是各个系统对这个格式支持也不是很统一的,我先后在ios和windows Phone上做了测试,在ios上会跳转到短信 ...

  9. android vcf iphone6,安卓通讯录导入到iphon最简单的方式(安卓通讯录导入iphon

    安卓手机里的通讯录怎么导入苹果手机上 1首先进入iphon桌面,找到设置"点击进入,2设置界面下拉菜单找到icloud选项,进入.3将"icloud中的通讯录"开关翻开, ...

最新文章

  1. 今年下半年,中日合拍的《Git游记》即将正式开机,我将...(上集)
  2. PHP CURL 哈哈哈哈哈记录一下
  3. Maven插件tomcat7-maver-plugin
  4. 网站建设案例欣赏_网站制作设计案例_成都辰星建站
  5. 【转载】【Python-ML】SKlearn库谱聚类SpectralClustering模型
  6. UA MATH567 高维统计IV Lipschitz组合2 Spherical Distribution的Lipschitz函数 Isoperimetric不等式
  7. hibernate ——联合主键
  8. selenium fluentwait java实例
  9. uva 1463 - Largest Empty Circle on a Segment(二分+三分+几何)
  10. 腐蚀rust图纸怎么找_怎么解决变压器油滤油机的温差效应?在这里可以得到解决...
  11. StuQ Android 会员学习计划|帮你成为更优秀的 Android 工程师
  12. 如何借助log4j把日志写入数据库中
  13. 漫画:Bitmap算法
  14. Qt第一章:pyside6安装与配置
  15. httpCient 的getResponseBodyAsStream instead is recommended使用方法
  16. 【综述笔记】一些弱监督语义分割论文
  17. 1419D2 Sage‘s Birthday
  18. 招商银行一网通支付(php接入招商银行一网通支付)
  19. 谷歌浏览器,查找CSS选择器
  20. 刚体运动学-四元数插值

热门文章

  1. leetcode 290. 单词规律(Java版)
  2. 网络与IO知识扫盲(七):仿照Netty工作架构图,手写多路复用模型
  3. 【C/C++】从技术学习和实际运用的角度来看,C/C++和Java到底区别在哪?C语言、C++学习路线?
  4. C++ KMP算法之next数组的生成
  5. C语言二维数组、参数传递的理解
  6. mybatis mapper.xml入参
  7. 多线程基础-常用线程方法(三)
  8. notify和wait
  9. 图文并茂的讲解 ICMP (网际控制报文)协议
  10. 洛谷--P1067 多项式输出