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;

	@Override
	protected 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() {

			@Override
			public 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() {

							@Override
							public 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;
			}
		});

	}

	// 创建baseadapter

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

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return infos.size();
		}

		@Override
		public Object getItem(int position) {

			return infos.get(position);
		}

		@Override
		public long getItemId(int position) {

			return 0;
		}

		@Override
		public 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;
		}

	}
}




郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。