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;
}

}
-------------------------------------------
實現效果爲:













發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章