Android通訊錄管理二之通話記錄擷取

來源:互聯網
上載者:User

標籤: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通訊錄管理二之通話記錄擷取

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.