当前位置: 首页 > news >正文

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

 

http://blog.csdn.net/wwj_748/article/details/19970271

 
Android通讯录管理(获取联系人、通话记录、短信消息)(二)
 分类:
【Android通讯录模块开发】(10) 

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

 

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

 

 

同样的,你可以到这里下载源码:http://download.csdn.net/detail/wwj_748/6962865

 

 

界面布局:

/Contact_Demo/res/layout/contact_record_list_view.xml

 

[html]  view plain copy 在CODE上查看代码片派生到我的代码片
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/contact_record_view"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"   
  6.     android:background="#000000">  
  7.   
  8.     <ListView  
  9.         android:id="@+id/call_log_list"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="fill_parent"  
  12.         android:layout_alignParentTop="true"  
  13.         android:cacheColorHint="#000000"  
  14.         android:fadingEdge="none"  
  15.         android:scrollingCache="false"  
  16.         android:visibility="visible" />  
  17.   
  18. </RelativeLayout>  

/Contact_Demo/res/layout/contact_record_list_item.xml

 

 

[html]  view plain copy 在CODE上查看代码片派生到我的代码片
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <ImageView  
  8.         android:id="@+id/call_type"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_centerVertical="true"  
  12.         android:layout_marginLeft="5dip"  
  13.         android:layout_marginRight="5dip"  
  14.         android:background="@drawable/ic_calllog_outgoing_nomal" />  
  15.   
  16.     <LinearLayout  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:layout_centerVertical="true"  
  20.         android:layout_toRightOf="@+id/call_type"  
  21.         android:orientation="vertical" >  
  22.   
  23.         <TextView  
  24.             android:id="@+id/name"  
  25.             android:layout_width="wrap_content"  
  26.             android:layout_height="0dip"  
  27.             android:layout_weight="1"  
  28.             android:textAppearance="?android:textAppearanceMedium"  
  29.             android:textColor="#ffffff" />  
  30.   
  31.         <TextView  
  32.             android:id="@+id/number"  
  33.             android:layout_width="wrap_content"  
  34.             android:layout_height="wrap_content"  
  35.             android:textAppearance="?android:textAppearanceSmall"  
  36.             android:textColor="#cccccc" />  
  37.     </LinearLayout>  
  38.   
  39.     <TextView  
  40.         android:id="@+id/call_btn"  
  41.         android:layout_width="wrap_content"  
  42.         android:layout_height="wrap_content"  
  43.         android:layout_alignParentRight="true"  
  44.         android:layout_centerVertical="true"  
  45.         android:layout_marginLeft="10dip"  
  46.         android:layout_marginRight="10dip"  
  47.         android:background="@drawable/ic_calllog_call_btn" />  
  48.   
  49.     <ImageView  
  50.         android:id="@+id/fg"  
  51.         android:layout_width="wrap_content"  
  52.         android:layout_height="75dip"  
  53.         android:layout_toLeftOf="@+id/call_btn"  
  54.         android:background="@drawable/black_bg" />  
  55.   
  56.     <TextView  
  57.         android:id="@+id/time"  
  58.         android:layout_width="wrap_content"  
  59.         android:layout_height="wrap_content"  
  60.         android:layout_centerVertical="true"  
  61.         android:layout_toLeftOf="@+id/fg"  
  62.         android:textColor="#ffffff" />  
  63.   
  64. </RelativeLayout>  

定义实体类:

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

 

[java]  view plain copy 在CODE上查看代码片派生到我的代码片
 
  1. package com.suntek.contact.model;  
  2.   
  3. /** 
  4.  * 通话记录实体类 
  5.  *  
  6.  * @author Administrator 
  7.  *  
  8.  */  
  9. public class CallLogBean {  
  10.   
  11.     private int id;  
  12.     private String name; // 名称  
  13.     private String number; // 号码  
  14.     private String date; // 日期  
  15.     private int type; // 来电:1,拨出:2,未接:3  
  16.     private int count; // 通话次数  
  17.   
  18.     public int getId() {  
  19.         return id;  
  20.     }  
  21.   
  22.     public void setId(int id) {  
  23.         this.id = id;  
  24.     }  
  25.   
  26.     public String getName() {  
  27.         return name;  
  28.     }  
  29.   
  30.     public void setName(String name) {  
  31.         this.name = name;  
  32.     }  
  33.   
  34.     public String getNumber() {  
  35.         return number;  
  36.     }  
  37.   
  38.     public void setNumber(String number) {  
  39.         this.number = number;  
  40.     }  
  41.   
  42.     public String getDate() {  
  43.         return date;  
  44.     }  
  45.   
  46.     public void setDate(String date) {  
  47.         this.date = date;  
  48.     }  
  49.   
  50.     public int getType() {  
  51.         return type;  
  52.     }  
  53.   
  54.     public void setType(int type) {  
  55.         this.type = type;  
  56.     }  
  57.   
  58.     public int getCount() {  
  59.         return count;  
  60.     }  
  61.   
  62.     public void setCount(int count) {  
  63.         this.count = count;  
  64.     }  
  65.   
  66. }  


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

 

[java]  view plain copy 在CODE上查看代码片派生到我的代码片
 
  1. package com.suntek.contact.adapter;  
  2.   
  3. import java.util.List;  
  4.   
  5. import android.content.Context;  
  6. import android.content.Intent;  
  7. import android.net.Uri;  
  8. import android.view.LayoutInflater;  
  9. import android.view.View;  
  10. import android.view.View.OnClickListener;  
  11. import android.view.ViewGroup;  
  12. import android.widget.BaseAdapter;  
  13. import android.widget.ImageView;  
  14. import android.widget.TextView;  
  15.   
  16. import com.suntek.contact.R;  
  17. import com.suntek.contact.model.CallLogBean;  
  18.   
  19. /** 
  20.  * 电话记录适配器 
  21.  *  
  22.  * @author Administrator 
  23.  *  
  24.  */  
  25. public class DialAdapter extends BaseAdapter {  
  26.   
  27.     private Context ctx;  
  28.     private List<CallLogBean> callLogs;  
  29.     private LayoutInflater inflater;  
  30.   
  31.     public DialAdapter(Context context, List<CallLogBean> callLogs) {  
  32.         this.ctx = context;  
  33.         this.callLogs = callLogs;  
  34.         this.inflater = LayoutInflater.from(context);  
  35.     }  
  36.   
  37.     @Override  
  38.     public int getCount() {  
  39.         return callLogs.size();  
  40.     }  
  41.   
  42.     @Override  
  43.     public Object getItem(int position) {  
  44.         return callLogs.get(position);  
  45.     }  
  46.   
  47.     @Override  
  48.     public long getItemId(int position) {  
  49.         return position;  
  50.     }  
  51.   
  52.     @Override  
  53.     public View getView(int position, View convertView, ViewGroup parent) {  
  54.         ViewHolder holder;  
  55.         if (convertView == null) {  
  56.             convertView = inflater.inflate(R.layout.contact_record_list_item,  
  57.                     null);  
  58.             holder = new ViewHolder();  
  59.             holder.call_type = (ImageView) convertView  
  60.                     .findViewById(R.id.call_type);  
  61.             holder.name = (TextView) convertView.findViewById(R.id.name);  
  62.             holder.number = (TextView) convertView.findViewById(R.id.number);  
  63.             holder.time = (TextView) convertView.findViewById(R.id.time);  
  64.             holder.call_btn = (TextView) convertView  
  65.                     .findViewById(R.id.call_btn);  
  66.             convertView.setTag(holder); // 缓存  
  67.         } else {  
  68.             holder = (ViewHolder) convertView.getTag();  
  69.         }  
  70.   
  71.         CallLogBean callLog = callLogs.get(position);  
  72.         switch (callLog.getType()) {  
  73.         case 1:  
  74.             holder.call_type  
  75.                     .setBackgroundResource(R.drawable.ic_calllog_outgoing_nomal);  
  76.             break;  
  77.         case 2:  
  78.             holder.call_type  
  79.                     .setBackgroundResource(R.drawable.ic_calllog_incomming_normal);  
  80.             break;  
  81.         case 3:  
  82.             holder.call_type  
  83.                     .setBackgroundResource(R.drawable.ic_calllog_missed_normal);  
  84.             break;  
  85.         }  
  86.         holder.name.setText(callLog.getName());  
  87.         holder.number.setText(callLog.getNumber());  
  88.         holder.time.setText(callLog.getDate());  
  89.   
  90.         addViewListener(holder.call_btn, callLog, position);  
  91.   
  92.         return convertView;  
  93.     }  
  94.   
  95.     private static class ViewHolder {  
  96.         ImageView call_type;  
  97.         TextView name;  
  98.         TextView number;  
  99.         TextView time;  
  100.         TextView call_btn;  
  101.     }  
  102.   
  103.     private void addViewListener(View view, final CallLogBean callLog,  
  104.             final int position) {  
  105.         view.setOnClickListener(new OnClickListener() {  
  106.   
  107.             @Override  
  108.             public void onClick(View v) {  
  109.                 Uri uri = Uri.parse("tel:" + callLog.getNumber());  
  110.                 Intent intent = new Intent(Intent.ACTION_CALL, uri);  
  111.                 ctx.startActivity(intent);  
  112.             }  
  113.         });  
  114.     }  
  115. }  

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

 

 

[java]  view plain copy 在CODE上查看代码片派生到我的代码片
 
  1. package com.suntek.contact;  
  2.   
  3. import java.text.SimpleDateFormat;  
  4. import java.util.ArrayList;  
  5. import java.util.Date;  
  6. import java.util.List;  
  7.   
  8. import android.app.Activity;  
  9. import android.content.AsyncQueryHandler;  
  10. import android.content.ContentResolver;  
  11. import android.database.Cursor;  
  12. import android.net.Uri;  
  13. import android.os.Bundle;  
  14. import android.provider.CallLog;  
  15. import android.widget.ListView;  
  16.   
  17. import com.suntek.contact.adapter.DialAdapter;  
  18. import com.suntek.contact.model.CallLogBean;  
  19.   
  20. /** 
  21.  * 通话记录列表 
  22.  *  
  23.  * @author wwj 
  24.  *  
  25.  */  
  26. public class ContactRecordListActivity extends Activity {  
  27.     private ListView callLogListView;  
  28.     private AsyncQueryHandler asyncQuery;  
  29.     private DialAdapter adapter;  
  30.     private List<CallLogBean> callLogs;  
  31.   
  32.     @Override  
  33.     protected void onCreate(Bundle savedInstanceState) {  
  34.         super.onCreate(savedInstanceState);  
  35.         setContentView(R.layout.contact_record_list_view);  
  36.   
  37.         callLogListView = (ListView) findViewById(R.id.call_log_list);  
  38.         asyncQuery = new MyAsyncQueryHandler(getContentResolver());  
  39.         init();  
  40.     }  
  41.   
  42.     private void init() {  
  43.         Uri uri = android.provider.CallLog.Calls.CONTENT_URI;  
  44.         // 查询的列  
  45.         String[] projection = { CallLog.Calls.DATE, // 日期  
  46.                 CallLog.Calls.NUMBER, // 号码  
  47.                 CallLog.Calls.TYPE, // 类型  
  48.                 CallLog.Calls.CACHED_NAME, // 名字  
  49.                 CallLog.Calls._ID, // id  
  50.         };  
  51.         asyncQuery.startQuery(0, null, uri, projection, null, null,  
  52.                 CallLog.Calls.DEFAULT_SORT_ORDER);  
  53.     }  
  54.   
  55.     private class MyAsyncQueryHandler extends AsyncQueryHandler {  
  56.   
  57.         public MyAsyncQueryHandler(ContentResolver cr) {  
  58.             super(cr);  
  59.         }  
  60.   
  61.         @Override  
  62.         protected void onQueryComplete(int token, Object cookie, Cursor cursor) {  
  63.             if (cursor != null && cursor.getCount() > 0) {  
  64.                 callLogs = new ArrayList<CallLogBean>();  
  65.                 SimpleDateFormat sfd = new SimpleDateFormat("MM-dd hh:mm");  
  66.                 Date date;  
  67.                 cursor.moveToFirst(); // 游标移动到第一项  
  68.                 for (int i = 0; i < cursor.getCount(); i++) {  
  69.                     cursor.moveToPosition(i);  
  70.                     date = new Date(cursor.getLong(cursor  
  71.                             .getColumnIndex(CallLog.Calls.DATE)));  
  72.                     String number = cursor.getString(cursor  
  73.                             .getColumnIndex(CallLog.Calls.NUMBER));  
  74.                     int type = cursor.getInt(cursor  
  75.                             .getColumnIndex(CallLog.Calls.TYPE));  
  76.                     String cachedName = cursor.getString(cursor  
  77.                             .getColumnIndex(CallLog.Calls.CACHED_NAME));// 缓存的名称与电话号码,如果它的存在  
  78.                     int id = cursor.getInt(cursor  
  79.                             .getColumnIndex(CallLog.Calls._ID));  
  80.   
  81.                     CallLogBean callLogBean = new CallLogBean();  
  82.                     callLogBean.setId(id);  
  83.                     callLogBean.setNumber(number);  
  84.                     callLogBean.setName(cachedName);  
  85.                     if (null == cachedName || "".equals(cachedName)) {  
  86.                         callLogBean.setName(number);  
  87.                     }  
  88.                     callLogBean.setType(type);  
  89.                     callLogBean.setDate(sfd.format(date));  
  90.   
  91.                     callLogs.add(callLogBean);  
  92.                 }  
  93.                 if (callLogs.size() > 0) {  
  94.                     setAdapter(callLogs);  
  95.                 }  
  96.             }  
  97.             super.onQueryComplete(token, cookie, cursor);  
  98.         }  
  99.     }  
  100.   
  101.     private void setAdapter(List<CallLogBean> callLogs) {  
  102.         adapter = new DialAdapter(this, callLogs);  
  103.         callLogListView.setAdapter(adapter);  
  104.     }  
  105. }  


 

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

 

[java]  view plain copy 在CODE上查看代码片派生到我的代码片
 
  1. <pre code_snippet_id="205549" snippet_file_name="blog_20140226_3_3338512"></pre>  
  2. <pre></pre>  
  3. <pre></pre>  

转载于:https://www.cnblogs.com/mochaMM/p/5157283.html

相关文章:

  • h5开发坑点小总结
  • 在没有数据库表或者列的情况下新建model;rails ,ruby, rack
  • Keepalived
  • Nginx禁止ip访问或IP网段访问方法
  • Investigating Your RAM Usage
  • Java迭代器spliterator
  • Oracle TDE的学习
  • CSS 中 calc() 函数用法
  • springsecurity源码查看网址
  • Mod in math
  • js keyup、keypress和keydown事件 详解
  • 云栖问答送的淘公仔收到啦
  • struts2自己定义类型转换器
  • DJANGO的requirements的运用
  • 糖葫芦照样吃
  • 002-读书笔记-JavaScript高级程序设计 在HTML中使用JavaScript
  • C# 免费离线人脸识别 2.0 Demo
  • Java 网络编程(2):UDP 的使用
  • JavaScript 事件——“事件类型”中“HTML5事件”的注意要点
  • Spring Cloud中负载均衡器概览
  • swift基础之_对象 实例方法 对象方法。
  • 服务器从安装到部署全过程(二)
  • 复习Javascript专题(四):js中的深浅拷贝
  • 简单数学运算程序(不定期更新)
  • 面试总结JavaScript篇
  • 入门级的git使用指北
  • 腾讯视频格式如何转换成mp4 将下载的qlv文件转换成mp4的方法
  • 通过获取异步加载JS文件进度实现一个canvas环形loading图
  • 微信小程序设置上一页数据
  • ​一些不规范的GTID使用场景
  • #中国IT界的第一本漂流日记 传递IT正能量# 【分享得“IT漂友”勋章】
  • (13)Hive调优——动态分区导致的小文件问题
  • (html转换)StringEscapeUtils类的转义与反转义方法
  • (接口自动化)Python3操作MySQL数据库
  • (十)【Jmeter】线程(Threads(Users))之jp@gc - Stepping Thread Group (deprecated)
  • (原創) 如何使用ISO C++讀寫BMP圖檔? (C/C++) (Image Processing)
  • (转载)微软数据挖掘算法:Microsoft 时序算法(5)
  • (自适应手机端)响应式新闻博客知识类pbootcms网站模板 自媒体运营博客网站源码下载
  • .htaccess配置重写url引擎
  • .L0CK3D来袭:如何保护您的数据免受致命攻击
  • .NET Compact Framework 3.5 支持 WCF 的子集
  • .NET 常见的偏门问题
  • .net 设置默认首页
  • .net 怎么循环得到数组里的值_关于js数组
  • .so文件(linux系统)
  • @font-face 用字体画图标
  • @PreAuthorize注解
  • @TableId注解详细介绍 mybaits 实体类主键注解
  • @Transactional类内部访问失效原因详解
  • [ 2222 ]http://e.eqxiu.com/s/wJMf15Ku
  • [ Algorithm ] N次方算法 N Square 动态规划解决
  • [ C++ ] template 模板进阶 (特化,分离编译)
  • [AndroidStudio]_[初级]_[修改虚拟设备镜像文件的存放位置]
  • [BT]BUUCTF刷题第9天(3.27)
  • [Godot] 3D拾取