获取手机通讯录在开发中经常会用到,这边记下日后查看。
1、获取手机通讯录是要取得系统权限的将下面权限添加到AndroidManifest文件中(我经常在权限问题上吃亏,经常代码写好了,也没有错,就是出不来效果,所以以后需求中用到权限的,先在配置文件中把权限添加进去):
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.READ_CALL_LOG"/>
<uses-permission android:name="android.permission.WRITE_CALL_LOG"/>
2、通话记录的Bean:CallLogBean
public class CallLogBean {
private int id;
private String name; //名字
private String number; //手机号
private String date; //日期
private int type; //类型 1,来电 2,呼出 3,未接
private int count; //通话次数
private String phonelocal;//号码归属地
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
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;
}
public String getPhonelocal() {
return phonelocal;
}
public void setPhonelocal(String phonelocal) {
this.phonelocal = phonelocal;
}
}
3. 在Activity 声明AsyncQueryHandler ,新建一个内部类:
代码里面有注释,我也不多解释,不明白AsyncQueryHandler是什么的朋友可以百度下。
private AsyncQueryHandler asyncQueryHandler;
/**
* 通话记录整理
* @author Administrator
*
*/
private boolean isQuerying;
private class MyAsyncQueryHandler extends AsyncQueryHandler{
@SuppressLint("HandlerLeak")
public MyAsyncQueryHandler(ContentResolver cr) {
super(cr);
// TODO Auto-generated constructor stub
}
protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
// TODO Auto-generated method stub
try{
if(cursor != null && cursor.getCount() >0){
callBeanList = new ArrayList<CallLagBean>();
cursor.moveToNext();
for(int i = 0; i<cursor.getCount(); i++){
cursor.moveToPosition(i);
//取出数据
Long date = cursor.getLong(cursor.getColumnIndex(CallLog.Calls.DATE));
int id = cursor.getInt(cursor.getColumnIndex(CallLog.Calls._ID));
String cachedName = cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME));
int type = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.TYPE));
String number = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
//封装bean
CallLagBean callLagBean = new CallLagBean();
callLagBean.setId(id);
callLagBean.setName(cachedName);
callLagBean.setNumber(number);
callLagBean.setType(type);
if(cachedName == null || cachedName.equals("")){
callLagBean.setName(number);
}
Calendar calendarNow = Calendar.getInstance();
Calendar callTime = Calendar.getInstance();
callTime.setTimeInMillis(date);
int hourOfDay = callTime.get(Calendar.HOUR_OF_DAY); //时
String hourStr = (hourOfDay > 10) ? hourOfDay +"" : "0"+hourOfDay;
int minute = callTime.get(Calendar.MINUTE);
String minuteStr = (minute > 10)? minute + "" : "0" + minute;
callTime.setTimeInMillis(date);
long now = calendarNow.getTimeInMillis();
if(now - (60 * 1000) <date){ //小于一分钟 显示刚刚
callLagBean.setDate("刚刚");
}else if(now - (60 * 60 * 1000)< date){ //一小时内 显示时间
callLagBean.setDate(Long.valueOf(now - date) / (1000 * 6) +"分钟前");
}else if(calendarNow.get(Calendar.MONTH) == callTime.get(Calendar.MONTH) //显示今天
&& calendarNow.get(Calendar.DAY_OF_MONTH) == callTime.get(Calendar.DAY_OF_MONTH)
){
callLagBean.setDate("今天"+hourStr+":"+minuteStr);
}else{ //月日:时分
callLagBean.setDate((callTime.get(Calendar.MONTH) + 1)
+ "月" + callTime.get(Calendar.DAY_OF_MONTH)
+ "日" + " " + hourStr + ":" + minuteStr);
}
callBeanList.add(callLagBean);
}
Long start = System.currentTimeMillis();
System.out.println("==============================》开始整理通话记录");
callBeanList = sortCallLogs(callBeanList);
System.out.println("==========整理耗时:"+(System.currentTimeMillis() - start));
System.out.println("==========>通话记录整理完毕");
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
isQuerying = false;
}
}, 100);
}else{
Toast.makeText(HomeDialActivity.this, "通话记录为空", 1).show();
}
}catch(Exception e){
Log.e("HomeDailgActivity", e.toString());
}finally{
if(cursor != null){
cursor.close();
}
}
super.onQueryComplete(token, cookie, cursor);
}
}
4、查询通讯录的方法:
/**
* 查询通讯录
*/
private void initQuery() {
Uri uri = CallLog.Calls.CONTENT_URI; //通讯录的uri
String[] projection = { CallLog.Calls.DATE, CallLog.Calls.NUMBER,
CallLog.Calls.TYPE, CallLog.Calls.CACHED_NAME,
CallLog.Calls._ID
// "geocoded_location"
}; // 查询的列
asyncQueryHandler.startQuery(0, null, uri, projection, null, null,CallLog.Calls.DEFAULT_SORT_ORDER);
}
5、在Activity的onCreate中分别调用:
asyncQueryHandler = new MyAsyncQueryHandler(getContentResolver());
initQuery();
这个时候就取出了手机的所有通讯录,都在callBeanList里面,想要看具体的某一条可以设断点