Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android通訊錄管理二之通話記錄獲取

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

編輯:關於Android編程

上一篇博客講的是獲取聯系人信息,這篇是獲取通話記錄

\

 

 

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 getCallLog(Context context) {
		List infos = new ArrayList();
		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 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 infos;
		private LayoutInflater inflater;

		public MyAdapter(List 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;
		}

	}
}




 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved