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


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


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


界面布局:

/Contact_Demo/res/layout/contact_record_list_view.xml

<?xml version="1.0" encoding="utf-8"?>    

/Contact_Demo/res/layout/contact_record_list_item.xml

<?xml version="1.0" encoding="utf-8"?>                                        

定义实体类:

/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,未接:3private 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);}@Overridepublic int getCount() {return callLogs.size();}@Overridepublic Object getItem(int position) {return callLogs.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic 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() {@Overridepublic 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;@Overrideprotected 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);}@Overrideprotected 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来取得数据库对应表中的字段值。


   
   


更多相关文章

  1. Android(安卓)LitePal介绍与使用说明
  2. okhttp3的基本使用
  3. Android开发——SQLite数据库(二)android studio创建数据库,进行插
  4. Android实现一款不错Banner界面广告图片循环轮播
  5. Android(安卓)Adapter中的getView缓存失效
  6. GreenDao数据库升级解决方案
  7. Android(安卓)程序员优选 数据库辅助工具 NaviCat
  8. Android(安卓)6种快速开发框架
  9. ListView拖动时背景黑色的问题

随机推荐

  1. PHP:【微信小程序】微信小程序数据交互,微
  2. PHP:【微信小程序】初识微信小程序,微信小
  3. 浅析 URL
  4. 微信小程序介绍、配置、视图层、逻辑层、
  5. 热烈庆祝!!!经过不懈努力,荣登前八名热门博主
  6. 为何电脑磁盘有的文件突然都看不见了,却
  7. 仿php中文网
  8. 选择器的使用和模块化组件思想
  9. PHP:【微信小程序】微信小程序API,微信小
  10. 请求与响应