android实习程序7——通话记录显示
下载SQLiteSpy.exe
打开模拟器5554
打开perspective,选择DDMS
打开Devices,确认存在emulator-5554
打开file Explorer
打开data文件夹
打开data 文件夹
打开com.android.providers.telephony文件夹
打开database文件夹
导出mmssms.db 和 telephony.db
可用SQLiteSpy.exe打开mmssms.db 和 telephony.db
打开sms文件夹,可看到联系人记录
转换到java模式,打开manifest.xml
选择permission
添加用户权限userpermission
分别选择:
android.permission.READ_CONTACTS
android.permission.READ_SMS
android.permission.CALL_PHONE
此时可看到 manifest.xml 文件增加的内容为:
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
腾讯有多可怕:
如果想添加一个知道电话号码的QQ好友
先在手机上添加此联系人的电话
(可能需要等上两小时)PC上随便加个好友,出现好友推荐界面,第一行--第二行必定有新加入的手机电话号码的人员的QQ
微信、QQ、短信存在关键字过滤——智能手机基本无秘密所言(权限的允许)
打开file Explorer
打开data文件夹
打开data 文件夹
打开com.android.providers.contacts文件夹
打开database文件夹
导出contact2.db
用SQLiteSpy.exe打开contact2.db
provider是android的四大组件之一
provider是实现跨应用程序访问数据的组件
如:com.android.providers.media
不会自定义provider
provider的读取:getcontentResolver
URI 是URL 的超级
在URI 中头部可以自定义,不一定非得是http、ftp等协议
provider像是服务器
Resolver像是浏览器
靠 URI 的数据交互
项目经理、产品经理
48dp原则:通用
android:padding="4dp" 内缩
可用来设置某些文字在图片中的位置
基线对齐 android:layout_alignBaseline="@id/call_log_item_name"
可用DDMS中的emulator Control打电话给模拟器5554
Map 查找无序数据效率较高,支持put (key,Value)
装换为List是一个无序的链表
List
Set
1、在calllog日志中查看通话记录
2、通话记录的浏览(watch?)
3、通话记录的显示
4、可根据通话而改变的通话记录(通话次数+1及提前)
===============================
activity.java
package cpm.tarena.Phone;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class PhoneManagerActivity extends Activity {
//保存通话记录数据list
ContentResolver mContentResolver;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mContentResolver= this.getContentResolver();
// mContentResolver.insert(url, values);
// mContentResolver.delete(url, where, selectionArgs);
//mContentResolver.query(uri, projection, selection, selectionArgs, sortOrder);
//mContentResolver.query(CallLog.CONTENT_URI, null, "name== '?' and type == ?", String[] ("abc","1"), "date");
//游标,读取游标数据
Cursor mCursor= mContentResolver.query(CallLog.Calls.CONTENT_URI, null, null, null, "date");
String[] colsName = mCursor.getColumnNames();
while(mCursor.moveToNext()){
for(int i=0; i<colsName.length; i++){
Log.i("CallLog",colsName[i]+":"+mCursor.getString(i) );
}
Log.i("CallLog", "-------------------");
//String callNumber = mCursor.getString(mCursor.getColumnIndex("number"));
}
}
}
添加联系人,并打出电话
可查看log日志
可新建一个CallLog,标签设为CallLog
可看到通话的联系人姓名、电话号码、通话时长等
==========================================
2、通话记录的浏览
activity.java
package cpm.tarena.Phone;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class PhoneManagerActivity extends Activity {
//保存通话记录数据list
ContentResolver mContentResolver;
ArrayList<MyCallLog> mCallLogListData = new ArrayList<MyCallLog>();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mContentResolver= this.getContentResolver();
// mContentResolver.insert(url, values);
// mContentResolver.delete(url, where, selectionArgs);
//mContentResolver.query(uri, projection, selection, selectionArgs, sortOrder);
//mContentResolver.query(CallLog.CONTENT_URI, null, "name== '?' and type == ?", String[] ("abc","1"), "date");
//游标,读取游标数据
Cursor mCursor= mContentResolver.query(CallLog.Calls.CONTENT_URI,new String[] {"_id","date","duration","name","number","type"} , null, null, "date desc");
String[] colsName = mCursor.getColumnNames();
DateFormat dateFormat;
while(mCursor.moveToNext()){
MyCallLog nowCallLogs = new MyCallLog();
nowCallLogs.setId(mCursor.getInt(0));
nowCallLogs.setDate(mCursor.getLong(1));
Date nowdate = new Date(0);
if(nowdate.getTime() - (60*1000) < nowCallLogs.getDate()){
//显示刚刚
nowCallLogs.setDateFormat("刚刚");
}
else if (nowdate.getTime() - (60*1000*60) < nowCallLogs.getDate()){
//显示一小时内
nowCallLogs.setDateFormat("n分钟");
}
else if (nowdate.getTime() - (60*1000*60*24) < nowCallLogs.getDate()){
//显示今天
nowCallLogs.setDateFormat("今天");
}
else if (nowdate.getTime() - (60*1000*60*24*2) < nowCallLogs.getDate()){
//显示昨天
nowCallLogs.setDateFormat("昨天");
}
else{
// 月日 : 时分
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd hh:mm");
nowCallLogs.setDateFormat(sdf.format(new Date(nowCallLogs.getDate())));
}
nowCallLogs.setDuration(mCursor.getLong(2));
nowCallLogs.setName(mCursor.getString(3));
nowCallLogs.setTelNumber(mCursor.getString(4));
nowCallLogs.setType(mCursor.getInt(5));
mCallLogListData.add(nowCallLogs);
}
mCallLogListData.toString();
}
}
----------------------------
MyCallLog.java
package cpm.tarena.Phone;
import java.util.ArrayList;
import android.content.ContentResolver;
import android.database.Cursor;
import android.provider.CallLog;
import android.text.format.DateFormat;
import android.util.Log;
public class MyCallLog {
//id、时间、时长、打进 打出 未接(1 2 3)、号码。。。
private int id;
private long duration;
private long date;
private int type;
private String number;
private String name;
private String dateFormat;
public String getDateFormat() {
return dateFormat;
}
public void setDateFormat(String dateFormat) {
this.dateFormat = dateFormat;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public long getDuration() {
return duration;
}
public void setDuration(long duration) {
this.duration = duration;
}
public long getDate() {
return date;
}
public void setDate(long date) {
this.date = date;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getTelNumber() {
return number;
}
public void setTelNumber(String telNumber) {
this.number = telNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
===========================================
3、通话记录的显示
PhoneManager.java
package cpm.tarena.Phone;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
public class PhoneManagerActivity extends Activity {
//保存通话记录数据list
ContentResolver mContentResolver;
ArrayList<MyCallLog> mCallLogListData = new ArrayList<MyCallLog>();
ListView mCallLogListView;
CalllogAdapter mCallLogAdapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mContentResolver= this.getContentResolver();
// mContentResolver.insert(url, values);
// mContentResolver.delete(url, where, selectionArgs);
//mContentResolver.query(uri, projection, selection, selectionArgs, sortOrder);
//mContentResolver.query(CallLog.CONTENT_URI, null, "name== '?' and type == ?", String[] ("abc","1"), "date");
//游标,读取游标数据
Cursor mCursor= mContentResolver.query(CallLog.Calls.CONTENT_URI,
new String[] {"_id","date","duration","name","number","type"} , null, null, "date desc");
String[] colsName = mCursor.getColumnNames();
DateFormat dateFormat;
while(mCursor.moveToNext()){
MyCallLog nowCallLogs = new MyCallLog();
nowCallLogs.setId(mCursor.getInt(0));
nowCallLogs.setDate(mCursor.getLong(1));
Date nowdate = new Date(0);
if(nowdate.getTime() - (60*1000) < nowCallLogs.getDate()){
//显示刚刚
nowCallLogs.setDateFormat("刚刚");
}
else if (nowdate.getTime() - (60*1000*60) < nowCallLogs.getDate()){
//显示一小时内
nowCallLogs.setDateFormat("n分钟");
}
else if (nowdate.getTime() - (60*1000*60*24) < nowCallLogs.getDate()){
//显示今天
nowCallLogs.setDateFormat("今天");
}
else if (nowdate.getTime() - (60*1000*60*24*2) < nowCallLogs.getDate()){
//显示昨天
nowCallLogs.setDateFormat("昨天");
}
else{
// 月日 : 时分
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd hh:mm");
nowCallLogs.setDateFormat(sdf.format(new Date(nowCallLogs.getDate())));
}
nowCallLogs.setDuration(mCursor.getLong(2));
nowCallLogs.setName(mCursor.getString(3));
nowCallLogs.setTelNumber(mCursor.getString(4));
nowCallLogs.setType(mCursor.getInt(5));
mCallLogListData.add(nowCallLogs);
}
mCallLogListData.toString();
mCallLogListView = (ListView) findViewById(R.id.call_log_listview);
mCallLogAdapter = new CalllogAdapter(this, mCallLogListData, R.layout.calllog_list_item_layout);
mCallLogListView.setAdapter(mCallLogAdapter);
}
}
------------------------------------
MyCalllog.java
package cpm.tarena.Phone;
import java.util.ArrayList;
import android.content.ContentResolver;
import android.database.Cursor;
import android.provider.CallLog;
import android.text.format.DateFormat;
import android.util.Log;
public class MyCallLog {
ArrayList<CallLog> mCallLogs= new ArrayList<CallLog>();
//id、时间、时长、打进 打出 未接(1 2 3)、号码。。。
private int id;
private long duration;
private long date;
private int type;
private String number;
private String name;
private String dateFormat;
public String getDateFormat() {
return dateFormat;
}
public void setDateFormat(String dateFormat) {
this.dateFormat = dateFormat;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public long getDuration() {
return duration;
}
public void setDuration(long duration) {
this.duration = duration;
}
public long getDate() {
return date;
}
public void setDate(long date) {
this.date = date;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getTelNumber() {
return number;
}
public void setTelNumber(String telNumber) {
this.number = telNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
------------------------------------------------------------
CalllogAdaper.java
package cpm.tarena.Phone;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class CalllogAdapter extends BaseAdapter{
Context mcontext;
List<MyCallLog> mcalllogListData;
int mitemLayoutId;
LayoutInflater mLayoutInflater;
public CalllogAdapter(
Context context,
List<MyCallLog> calllogListData,
int itemLayoutId)
{
mcontext= context;
mcalllogListData= calllogListData;
mitemLayoutId= itemLayoutId;
mLayoutInflater = LayoutInflater.from(mcontext);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mcalllogListData.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mcalllogListData.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return mcalllogListData.get(position).hashCode();
}
@Override
public View getView(int position,
View convertView,
ViewGroup parent)
{
//判断是否实例化Item
if(convertView==null ){
convertView = mLayoutInflater.inflate(mitemLayoutId, null);
}
//找到需要修改的控件
TextView nameTv =(TextView) convertView.findViewById(R.id.call_log_item_name);
TextView numberTv =(TextView) convertView.findViewById(R.id.call_log_item_number);
TextView dateTv =(TextView) convertView.findViewById(R.id.call_log_item_date);
TextView typeTv =(TextView) convertView.findViewById(R.id.call_log_item_type);
//设置控件要显式的内同
//CalllogAdapter nowCallLog = mcalllogListData.get(position);
MyCallLog nowCallLogs = mcalllogListData.get(position);
if(null != nowCallLogs.getName()){
nameTv.setText(nowCallLogs.getName());
}
else{
nameTv.setText("未知");
}
if(null != nowCallLogs.getTelNumber()){
numberTv.setText(nowCallLogs.getTelNumber());
}
else{
numberTv.setText("保密");
}
dateTv.setText(nowCallLogs.getDateFormat());
switch (nowCallLogs.getType()) {
case 1:
//打进
typeTv.setBackgroundResource(R.drawable.icon_log_incoming);
break;
case 2:
//打出
typeTv.setBackgroundResource(R.drawable.icon_log_outgoing);
break;
case 3:
//未接
typeTv.setBackgroundResource(R.drawable.icon_log_missed);
break;
}
// TODO Auto-generated method stub
return convertView;
}
}
------------------------------------------------------------------------
calllog_list_item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="48dp"
android:orientation="vertical"
android:background="#ccfcfc">
<ImageView
android:id="@+id/call_log_item_photo"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="@drawable/default_thumbnail"
android:background="#00f"
android:padding="4dp"/>
<TextView
android:id="@+id/call_log_item_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/icon_log_missed"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="5"
android:textColor="#000"
android:gravity="center"
android:paddingRight="24dp"/>
<TextView
android:id="@+id/call_log_item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="张三"
android:paddingLeft="4dp"
android:paddingTop="4dp"
android:textSize="18dp"
android:textColor="#000"
android:layout_toRightOf="@id/call_log_item_photo"
android:layout_alignTop="@id/call_log_item_name"/>
<TextView
android:id="@+id/call_log_item_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="13717843343"
android:textSize="16dp"
android:paddingLeft="4dp"
android:layout_toRightOf="@id/call_log_item_name"
android:layout_alignBaseline="@id/call_log_item_name"/>
<TextView
android:id="@+id/call_log_item_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3小时前"
android:textSize="16dp"
android:paddingBottom="2dp"
android:paddingLeft="4dp"
android:layout_toRightOf="@id/call_log_item_photo"
android:layout_alignBottom="@id/call_log_item_photo"/>
</RelativeLayout>
----------------------------------------------------------------------
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@+id/call_log_listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
<LinearLayout
android:visibility="gone"
android:id="@+id/keyboard_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#fcfcfc"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/keyboard_line_bg" >
<ImageButton
android:id="@+id/hide_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#0000"
android:src="@drawable/keyboard_hide" />
<ImageButton
android:id="@+id/del_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#0000"
android:src="@drawable/keyboard_backspace" />
<Button
android:id="@+id/call_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/del_button"
android:layout_toRightOf="@id/hide_button"
android:background="@drawable/dial_call_bg"
android:ellipsize="start"
android:text="139111111"
android:textColor="#fff"
android:textSize="18sp" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/key_1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/key_bg_s"
android:src="@drawable/keyboard_1" />
<ImageButton
android:id="@+id/key_2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/key_bg_s"
android:src="@drawable/keyboard_2" />
<ImageButton
android:id="@+id/key_3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/key_bg_s"
android:src="@drawable/keyboard_3" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/key_4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/key_bg_s"
android:src="@drawable/keyboard_4" />
<ImageButton
android:id="@+id/key_5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/key_bg_s"
android:src="@drawable/keyboard_5" />
<ImageButton
android:id="@+id/key_6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/key_bg_s"
android:src="@drawable/keyboard_6" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/key_7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/key_bg_s"
android:src="@drawable/keyboard_7" />
<ImageButton
android:id="@+id/key_8"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/key_bg_s"
android:src="@drawable/keyboard_8" />
<ImageButton
android:id="@+id/key_9"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/key_bg_s"
android:src="@drawable/keyboard_9" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/key_star"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/key_bg_s"
android:src="@drawable/keyboard_star" />
<ImageButton
android:id="@+id/key_0"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/key_bg_s"
android:src="@drawable/keyboard_0" />
<ImageButton
android:id="@+id/key_hash"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/key_bg_s"
android:src="@drawable/keyboard_hash" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
=============================================
4、可根据通话而改变的通话记录(通话次数+1及提前)
PhoneManagerActivity.java
package cpm.tarena.Phone;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CallLog;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
public class PhoneManagerActivity extends Activity {
//保存通话记录数据list
ContentResolver mContentResolver;
ArrayList<MyCallLog> mCallLogListData = new ArrayList<MyCallLog>();
LinkedHashMap<String, MyCallLog> mCallLogMapData = new LinkedHashMap<String, MyCallLog>();
ListView mCallLogListView;
CalllogAdapter mCallLogAdapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mCallLogListView = (ListView) findViewById(R.id.call_log_listview);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
mContentResolver= this.getContentResolver();
//游标,读取游标数据
Cursor mCursor= mContentResolver.query(CallLog.Calls.CONTENT_URI,
new String[] {"_id","date","duration","name","number","type"} , null, null, "date desc");
String[] colsName = mCursor.getColumnNames();
DateFormat dateFormat;
//清空Map
mCallLogMapData.clear();
while( mCursor.moveToNext() ){
MyCallLog nowCallLogs = new MyCallLog();
nowCallLogs.setId(mCursor.getInt(0));
nowCallLogs.setDate(mCursor.getLong(1));
Date nowdate = new Date(0, 0, 0) ;
// 今天的凌晨
Date today = new Date(nowdate.getYear(), nowdate.getMonth(),nowdate.getDay());
// 时间转换规则
// 在1分钟内显示刚刚
// 60分钟内显示n分钟
// 超过60分钟,还在今天显示n小时之前
// 不在今天,在昨天24小时内显示 昨天 : 时分
// 超过昨天显示前天:时分
// 超过前天显示 月/日 时分
if(nowdate.getTime() - (60*1000) < nowCallLogs.getDate()){
//显示刚刚
nowCallLogs.setDateFormat("刚刚");
}
else if (nowdate.getTime() - (60*1000*60) < nowCallLogs.getDate()){
//显示一小时内
nowCallLogs.setDateFormat("n分钟");
}
else if (nowdate.getTime() - (60*1000*60*24) < nowCallLogs.getDate()){
//显示今天
nowCallLogs.setDateFormat("今天");
}
else if (nowdate.getTime() - (60*1000*60*24*2) < nowCallLogs.getDate()){
//显示昨天
nowCallLogs.setDateFormat("昨天");
}
else{
// 月日 : 时分
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd hh:mm");
nowCallLogs.setDateFormat(sdf.format(new Date(nowCallLogs.getDate())));
}
nowCallLogs.setDuration(mCursor.getLong(2));
nowCallLogs.setName(mCursor.getString(3));
nowCallLogs.setTelNumber(mCursor.getString(4));
nowCallLogs.setType(mCursor.getInt(5));
//mCallLogListData.add(nowCallLogs);
MyCallLog mapCallLog = mCallLogMapData.get(nowCallLogs.getTelNumber());
if(null != mapCallLog){
int callCount = mapCallLog.getCount()+1;
mapCallLog.setCount(callCount);
}
else{
nowCallLogs.setCount(1);
mCallLogMapData.put(nowCallLogs.getTelNumber(), nowCallLogs);
}
} //while的end
//将
mCallLogListData = new ArrayList<MyCallLog>(mCallLogMapData.values());
mCallLogListData.toString();
// mCallLogListView = (ListView) findViewById(R.id.call_log_listview);
mCallLogAdapter = new CalllogAdapter(this, mCallLogListData, R.layout.calllog_list_item_layout);
mCallLogListView.setAdapter(mCallLogAdapter);
mCallLogListView.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> listView, View itemView,
int position, long itemId) {
// TODO Auto-generated method stub
MyCallLog nowCallLog = (MyCallLog)listView.getItemAtPosition(position);
// 获得要拨打出去的电话
String number = nowCallLog.getTelNumber();
Uri callUri = Uri.parse("tel:" + number);
Intent callIntent = new Intent();
callIntent.setAction(Intent.ACTION_CALL);
callIntent.setData(callUri);
startActivity(callIntent);
}
});
}//onResume的end
}
----------------------------------------------
MyCallLog.java
package cpm.tarena.Phone;
import java.util.ArrayList;
import android.content.ContentResolver;
import android.database.Cursor;
import android.provider.CallLog;
import android.text.format.DateFormat;
import android.util.Log;
public class MyCallLog {
ArrayList<CallLog> mCallLogs= new ArrayList<CallLog>();
//id、时间、时长、打进 打出 未接(1 2 3)、号码。。。
private int id;
private long duration;
private long date;
private int type;
private String number;
private String name;
private String dateFormat;
private int count;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public String getDateFormat() {
return dateFormat;
}
public void setDateFormat(String dateFormat) {
this.dateFormat = dateFormat;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public long getDuration() {
return duration;
}
public void setDuration(long duration) {
this.duration = duration;
}
public long getDate() {
return date;
}
public void setDate(long date) {
this.date = date;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getTelNumber() {
return number;
}
public void setTelNumber(String telNumber) {
this.number = telNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
------------------------------------------
CalllogAdapter.java
package cpm.tarena.Phone;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class CalllogAdapter extends BaseAdapter{
Context mcontext;
List<MyCallLog> mcalllogListData;
int mitemLayoutId;
LayoutInflater mLayoutInflater;
public CalllogAdapter(
Context context,
List<MyCallLog> calllogListData,
int itemLayoutId)
{
mcontext= context;
mcalllogListData= calllogListData;
mitemLayoutId= itemLayoutId;
mLayoutInflater = LayoutInflater.from(mcontext);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mcalllogListData.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mcalllogListData.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return mcalllogListData.get(position).hashCode();
}
@Override
public View getView(
int position,
View convertView,
ViewGroup parent)
{
//判断是否实例化Item
if(convertView==null ){
convertView = mLayoutInflater.inflate(mitemLayoutId, null);
}
//找到需要修改的控件
TextView nameTv =(TextView) convertView.findViewById(R.id.call_log_item_name);
TextView numberTv =(TextView) convertView.findViewById(R.id.call_log_item_number);
TextView dateTv =(TextView) convertView.findViewById(R.id.call_log_item_date);
TextView typeTv =(TextView) convertView.findViewById(R.id.call_log_item_type);
//设置控件要显式的内容
MyCallLog nowCallLogs = mcalllogListData.get(position);
//显示联系人名称
if(null != nowCallLogs.getName()){
nameTv.setText(nowCallLogs.getName());
}
else{
nameTv.setText("未知");
}
//显示联系人电话号码
if(null != nowCallLogs.getTelNumber()){
numberTv.setText(nowCallLogs.getTelNumber());
}
else{
numberTv.setText("保密");
}
//显示通话记录时间
dateTv.setText(nowCallLogs.getDateFormat());
//显示通话类型(1:打进、2:打出、3:未接)
switch (nowCallLogs.getType()) {
case 1:
//打进
typeTv.setBackgroundResource(R.drawable.icon_log_incoming);
break;
case 2:
//打出
typeTv.setBackgroundResource(R.drawable.icon_log_outgoing);
break;
case 3:
//未接
typeTv.setBackgroundResource(R.drawable.icon_log_missed);
break;
}
//显示相同联系人的通话次数
typeTv.setText(""+nowCallLogs.getCount());
// TODO Auto-generated method stub
return convertView;
}
}
-------------------------------------------
实现效果为: