Android通讯录管理(获取联系人、通话记录、短信消息)(二

}

public void setName(String name) {

this.name = name;

}

public String getNumber() {

return number;

}

public void setNumber(String number) {

this.number = number;

}

public String getDate() {

return date;

}

public void setDate(String date) {

this.date = date;

}

public int getType() {

return type;

}

public void setType(int type) {

this.type = type;

}

public int getCount() {

return count;

}

public void setCount(int count) {

this.count = count;

}

}

/Contact_Demo/src/com/suntek/contact/adapter/DialAdapter.java

package com.suntek.contact.adapter;

import java.util.List;

import android.content.Context;

import android.content.Intent;

import android.net.Uri;

import android.view.LayoutInflater;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.ImageView;

import android.widget.TextView;

import com.suntek.contact.R;

import com.suntek.contact.model.CallLogBean;

/**

  • 电话记录适配器

  • @author Administrator

*/

public class DialAdapter extends BaseAdapter {

private Context ctx;

private List callLogs;

private LayoutInflater inflater;

public DialAdapter(Context context, List callLogs) {

this.ctx = context;

this.callLogs = callLogs;

this.inflater = LayoutInflater.from(context);

}

@Override

public int getCount() {

return callLogs.size();

}

@Override

public Object getItem(int position) {

return callLogs.get(position);

}

@Override

public long getItemId(int position) {

return position;

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder;

if (convertView == null) {

convertView = inflater.inflate(R.layout.contact_record_list_item,

null);

holder = new ViewHolder();

holder.call_type = (ImageView) convertView

.findViewById(R.id.call_type);

holder.name = (TextView) convertView.findViewById(R.id.name);

holder.number = (TextView) convertView.findViewById(R.id.number);

holder.time = (TextView) convertView.findViewById(R.id.time);

holder.call_btn = (TextView) convertView

.findViewById(R.id.call_btn);

convertView.setTag(holder); // 缓存

} else {

holder = (ViewHolder) convertView.getTag();

}

CallLogBean callLog = callLogs.get(position);

switch (callLog.getType()) {

case 1:

holder.call_type

.setBackgroundResource(R.drawable.ic_calllog_outgoing_nomal);

break;

case 2:

holder.call_type

.setBackgroundResource(R.drawable.ic_calllog_incomming_normal);

break;

case 3:

holder.call_type

.setBackgroundResource(R.drawable.ic_calllog_missed_normal);

break;

}

holder.name.setText(callLog.getName());

holder.number.setText(callLog.getNumber());

holder.time.setText(callLog.getDate());

addViewListener(holder.call_btn, callLog, position);

return convertView;

}

private static class ViewHolder {

ImageView call_type;

TextView name;

TextView number;

TextView time;

TextView call_btn;

}

private void addViewListener(View view, final CallLogBean callLog,

final int position) {

view.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

Uri uri = Uri.parse(“tel:” + callLog.getNumber());

Intent intent = new Intent(Intent.ACTION_CALL, uri);

ctx.startActivity(intent);

}

});

}

}

/Contact_Demo/src/com/suntek/contact/ContactRecordListActivity.java

package com.suntek.contact;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Date;

import java.util.List;

import android.app.Activity;

import android.content.AsyncQueryHandler;

import android.content.ContentResolver;

import android.database.Cursor;

import android.net.Uri;

import android.os.Bundle;

import android.provider.CallLog;

import android.widget.ListView;

import com.suntek.contact.adapter.DialAdapter;

import com.suntek.contact.model.CallLogBean;

/**

  • 通话记录列表

  • @author wwj

*/

public class ContactRecordListActivity extends Activity {

private ListView callLogListView;

private AsyncQueryHandler asyncQuery;

private DialAdapter adapter;

private List callLogs;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.contact_record_list_view);

callLogListView = (ListView) findViewById(R.id.call_log_list);

asyncQuery = new MyAsyncQueryHandler(getContentResolver());

init();

}

private void init() {

Uri uri = android.provider.CallLog.Calls.CONTENT_URI;

// 查询的列

String[] projection = { CallLog.Calls.DATE, // 日期

CallLog.Calls.NUMBER, // 号码

CallLog.Calls.TYPE, // 类型

CallLog.Calls.CACHED_NAME, // 名字

CallLog.Calls._ID, // id

};

asyncQuery.startQuery(0, null, uri, projection, null, null,

CallLog.Calls.DEFAULT_SORT_ORDER);

}

private class MyAsyncQueryHandler extends AsyncQueryHandler {

public MyAsyncQueryHandler(ContentResolver cr) {

super(cr);

}

@Override

protected void onQueryComplete(int token, Object cookie, Cursor cursor) {

if (cursor != null && cursor.getCount() > 0) {

callLogs = new ArrayList();

SimpleDateFormat sfd = new SimpleDateFormat(“MM-dd hh:mm”);

Date date;

cursor.moveToFirst(); // 游标移动到第一项

for (int i = 0; i < cursor.getCount(); i++) {

cursor.moveToPosition(i);

date = new Date(cursor.getLong(cursor

.getColumnIndex(CallLog.Calls.DATE)));

String number = cursor.getString(cursor

.getColumnIndex(CallLog.Calls.NUMBER));

int type = cursor.getInt(cursor

.getColumnIndex(CallLog.Calls.TYPE));

String cachedName = cursor.getString(cursor

.getColumnIndex(CallLog.Calls.CACHED_NAME));// 缓存的名称与电话号码,如果它的存在

int id = cursor.getInt(cursor

.getColumnIndex(CallLog.Calls._ID));

CallLogBean callLogBean = new CallLogBean();

callLogBean.setId(id);

callLogBean.setNumber(number);

最后

下面是辛苦给大家整理的学习路线

ring(cursor

.getColumnIndex(CallLog.Calls.NUMBER));

int type = cursor.getInt(cursor

.getColumnIndex(CallLog.Calls.TYPE));

String cachedName = cursor.getString(cursor

.getColumnIndex(CallLog.Calls.CACHED_NAME));// 缓存的名称与电话号码,如果它的存在

int id = cursor.getInt(cursor

.getColumnIndex(CallLog.Calls._ID));

CallLogBean callLogBean = new CallLogBean();

callLogBean.setId(id);

callLogBean.setNumber(number);

最后

下面是辛苦给大家整理的学习路线

[外链图片转存中…(img-akgqm41Z-1720084007623)]

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值