標籤:android 通訊錄 簡訊 通話記錄
上一篇部落格講的是擷取連絡人資訊,這篇是擷取通話記錄
同樣你可以在這裡下載http://download.csdn.net/detail/waniu123/8554533
package cn.zxw.contact.domain;/** * 通話記錄 * @author zhan * */public class CallLogInfo {public String number;public long date;public int type;public CallLogInfo(String number, long date, int type) {super();this.number = number;this.date = date;this.type = type;}public CallLogInfo() {super();}}
擷取通話記錄
/** * 擷取所有的通話記錄 * * @param context * @return */public List<CallLogInfo> getCallLog(Context context) {List<CallLogInfo> infos = new ArrayList<CallLogInfo>();ContentResolver cr = context.getContentResolver();Uri uri = Calls.CONTENT_URI;String[] projection = new String[] { Calls.NUMBER, Calls.DATE,Calls.TYPE };Cursor cursor = cr.query(uri, projection, null, null, null);while (cursor.moveToNext()) {String number = cursor.getString(0);long date = cursor.getLong(1);int type = cursor.getInt(2);infos.add(new CallLogInfo(number, date, type));}cursor.close();return infos;}activity中代碼
package cn.zxw.contact;import java.text.SimpleDateFormat;import java.util.List;import cn.zxw.contact.domain.CallLogInfo;import cn.zxw.contact.utils.ContactsMsgUtils;import android.app.Activity;import android.app.AlertDialog;import android.content.Context;import android.content.DialogInterface;import android.content.DialogInterface.OnClickListener;import android.content.Intent;import android.graphics.Color;import android.net.Uri;import android.os.Bundle;import android.provider.CallLog.Calls;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.AdapterView.OnItemLongClickListener;import android.widget.BaseAdapter;import android.widget.ListView;import android.widget.TextView;/** * 擷取通話記錄 * 根據電話類型分為 來電、去電、未接三種類型,顯示三種不同顏色標記 * 長按號碼,彈出對話方塊選擇:複製電話號碼到撥號盤, 撥號, 傳送簡訊 * @author zhan * */public class CallLogActivity extends Activity {private ListView lv;private MyAdapter adapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_contacts_msg_calllog);lv = (ListView) findViewById(R.id.lv);ContactsMsgUtils contactsMsgUtils = new ContactsMsgUtils();List<CallLogInfo> infos = contactsMsgUtils.getCallLog(this);adapter = new MyAdapter(infos);lv.setAdapter(adapter);lv.setOnItemLongClickListener(new OnItemLongClickListener() {@Overridepublic boolean onItemLongClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {CallLogInfo info = (CallLogInfo) adapter.getItem(arg2);final String number = info.number;String[] items = new String[] { "複製電話號碼到撥號盤", "撥號", "傳送簡訊" };new AlertDialog.Builder(CallLogActivity.this).setTitle("操作").setItems(items, new OnClickListener() {@Overridepublic void onClick(DialogInterface dialog,int which) {switch (which) {case 0:// 複製電話號碼到撥號盤startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + number)));break;case 1:// 撥號 許可權startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + number)));break;case 2:// 傳送簡訊startActivity(new Intent(Intent.ACTION_SENDTO, Uri.parse("sms:" + number)));break;default:break;}}}).show();return false;}});}// 建立baseadapterprivate class MyAdapter extends BaseAdapter {private List<CallLogInfo> infos;private LayoutInflater inflater;public MyAdapter(List<CallLogInfo> infos) {super();this.infos = infos;inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn infos.size();}@Overridepublic Object getItem(int position) {return infos.get(position);}@Overridepublic long getItemId(int position) {return 0;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {View view = inflater.inflate(R.layout.call_log_item, null);TextView tv_number = (TextView) view.findViewById(R.id.tv_number);TextView tv_date = (TextView) view.findViewById(R.id.tv_date);TextView tv_type = (TextView) view.findViewById(R.id.tv_type);// 建立一個對象CallLogInfo info = infos.get(position);tv_number.setText(info.number);// 格式時間SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");String dateStr = format.format(info.date);tv_date.setText(dateStr);String typeStr = null;int color = 0;switch (info.type) {case Calls.INCOMING_TYPE:typeStr = "來電";color = Color.BLUE;break;case Calls.OUTGOING_TYPE:typeStr = "去電";color = Color.GREEN;break;case Calls.MISSED_TYPE:typeStr = "未接";color = Color.RED;break;default:break;}tv_type.setText(typeStr);tv_type.setTextColor(color);return view;}}}
Android通訊錄管理二之通話記錄擷取