package com.noonecode.contentresolvercalllogdemo;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity {
private ListView mLvShow;
private List<Map<String, String>> dataList;
private SimpleAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLvShow = (ListView) findViewById(R.id.lv_show);
dataList = getDataList();
adapter = new SimpleAdapter(this, dataList, R.layout.simple_calllog_item//
, new String[] { "name", "number", "date", "duration", "type" }//
, new int[] { R.id.tv_name, R.id.tv_number, R.id.tv_date, R.id.tv_duration, R.id.tv_type });
mLvShow.setAdapter(adapter);
}
/**
* 读取数据
*
* @return 读取到的数据
*/
private List<Map<String, String>> getDataList() {
// 1.获得ContentResolver
ContentResolver resolver = getContentResolver();
// 2.利用ContentResolver的query方法查询通话记录数据库
/**
* @param uri 需要查询的URI,(这个URI是ContentProvider提供的)
* @param projection 需要查询的字段
* @param selection sql语句where之后的语句
* @param selectionArgs ?占位符代表的数据
* @param sortOrder 排序方式
*
*/
Cursor cursor = resolver.query(CallLog.Calls.CONTENT_URI, // 查询通话记录的URI
new String[] { CallLog.Calls.CACHED_NAME// 通话记录的联系人
, CallLog.Calls.NUMBER// 通话记录的电话号码
, CallLog.Calls.DATE// 通话记录的日期
, CallLog.Calls.DURATION// 通话时长
, CallLog.Calls.TYPE }// 通话类型
, null, null, CallLog.Calls.DEFAULT_SORT_ORDER// 按照时间逆序排列,最近打的最先显示
);
// 3.通过Cursor获得数据
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME));
String number = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
long dateLong = cursor.getLong(cursor.getColumnIndex(CallLog.Calls.DATE));
String date = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(new Date(dateLong));
int duration = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.DURATION));
int type = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.TYPE));
String typeString = "";
switch (type) {
case CallLog.Calls.INCOMING_TYPE:
typeString = "打入";
break;
case CallLog.Calls.OUTGOING_TYPE:
typeString = "打出";
break;
case CallLog.Calls.MISSED_TYPE:
typeString = "未接";
break;
default:
break;
}
Map<String, String> map = new HashMap<String, String>();
map.put("name", (name == null) ? "未备注联系人" : name);
map.put("number", number);
map.put("date", date);
map.put("duration", (duration / 60) + "分钟");
map.put("type", typeString);
list.add(map);
}
return list;
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.noonecode.contentresolvercalllogdemo.MainActivity" >
<ListView
android:id="@+id/lv_show"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="name"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:text="number"
/>
<TextView
android:id="@+id/tv_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:text="date"
/>
<TextView
android:id="@+id/tv_duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:text="duration"
/>
<TextView
android:id="@+id/tv_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:text="type"
/>
</LinearLayout>
</LinearLayout>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有